Question

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?

Was it helpful?

Solution

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

OTHER TIPS

check your spelling..

fondetcaption.length

Did you try:

var howmany = fondetcaption.length;

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

Try length.

alert(fondetcaption.length);

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.

This returns the array's length: fondetcaption.length

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top