-1

I have a 2 dimension array :

var fondetcaption = [
        ["fond/fond1.jpg","« aaa"],    
        ["fond/fond2.jpg","« bbb"],
        ["fond/fond3.jpg","« ccc"],
        ["fond/fond4.jpg","« ddd"]          
    ];

That array can have 4, 7, 10, any number of value in...

I like to know how many pair of value I have (it should return 4 in this case)

var howmany = fondetcaption.lenght; // doesn't work !

And after that... i will show fondetcaption[0][0] (the first background) and after that on click on a button i like to show the next : [1][0] and then next [2][0] and then [3][0] and then [0][0] again.... push doesn't seem to work.

Any idea?

1
  • Where do you use the array? because it works for me i.imgur.com/QIiGy.png .Your array is defined in a local scope so that might be your problem. Commented Jan 26, 2011 at 22:07

7 Answers 7

5

check your spelling..

fondetcaption.length
Sign up to request clarification or add additional context in comments.

Comments

4

As the others have said, it's length, not lenght.

But no one seems to have addressed the second part of your question, so:

You don't need push to cycle through the values. All you need is an index:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
var fondetcaptionIndex = 0;

// Call this when you click your button or whatever
function getNextBackground() {
    if (fondetcaptionIndex >= fondetcaption.length) {
        fondetcaptionIndex = 0;
    }
    return fondetcaption[fondetcaptionIndex++];
}

Or, if you like, you can just put the index directly on the array object, since JavaScript array objects can have arbitrary non-element properties and that helps keep the symbols together:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
fondetcaption.index = 0;

// Call this when you click your button or whatever
function getNextBackground() {
    if (fondetcaption.index >= fondetcaption.length) {
        fondetcaption.index = 0;
    }
    return fondetcaption[fondetcaption.index++];
}

In fact, you can even make the function part of the array:

var fondetcaption = [
    ["fond/fond1.jpg","« aaa"],    
    ["fond/fond2.jpg","« bbb"],
    ["fond/fond3.jpg","« ccc"],
    ["fond/fond4.jpg","« ddd"]          
];
fondetcaption.index = 0;
fondetcaption.getNext = function() {
    if (this.index >= this.length) {
        this.index = 0;
    }
    return this[this.index++];
};

// Use
background = fondetcaption.getNext();

If making the array itself the container of those extra properties bothers you (it bothers some people), wrap the whole thing up in an object:

var fondetcaption = (function() {
    var index = 0,
        values = [
            ["fond/fond1.jpg","« aaa"],    
            ["fond/fond2.jpg","« bbb"],
            ["fond/fond3.jpg","« ccc"],
            ["fond/fond4.jpg","« ddd"]          
        ];

    function fondetcaption_getNext() {
        if (index >= values.length) {
            index = 0;
        }
        return values[index++];
    }

    return {
        values:  values,
        getNext: fondetcaption_getNext
    };
})();

// Sample use:
background = fondetcaption.getNext();

// Original array still accessible if desired as fondetcaption.values

1 Comment

you read the whole question and answer right (base on other answer) no wonder you have 43.4k under the hood !.. thanks
3

Did you try:

var howmany = fondetcaption.length;

Your original post has a spelling error, lenght needs to be length.

Comments

3

Try length.

alert(fondetcaption.length);

Comments

1

Not sure what's wrong since it works fine here..

var fondetcaption = [
        ["fond/fond1.jpg","« aaa"],    
        ["fond/fond2.jpg","« bbb"],
        ["fond/fond3.jpg","« ccc"],
        ["fond/fond4.jpg","« ddd"]          
];

alert(fondetcaption.length); 

http://jsfiddle.net/tLPwp/

Perhaps fondetcaption.lenght is actually copied from code, and thusly spelled incorrectly.

Comments

1

This returns the array's length: fondetcaption.length

Comments

0

You have a typo in code. It should be var howmany = fondetcaption.length, and not ... fondetcaption.lenght.

I suggest you use tools like Firebug's, Chrome's, or Safari's Console. It can be very helpful, especially with the problems like these. I copy-pasted your code, tried it, and it works like charm. With the mentioned typo fixed, of course.

Cheers.

1 Comment

Five previous people already gave this answer (well six, but you can't see the one who realized they were duplicating and deleted). Why add yet another one? The debugging comment is valid, but it's a comment, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.