Domanda
Ho un array di 2 dimensioni:
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
Questa matrice può avere 4, 7, 10, qualunque numero di valore nel ...
Mi piace sapere quanti coppia di valore ho (deve restituire 4 in questo caso)
var howmany = fondetcaption.lenght; // doesn't work !
E dopo ... Vi mostrerò fondetcaption[0][0]
(il primo sfondo)
e dopo che al clic su un pulsante mi piace mostrare il prossimo: [1][0]
e poi la prossima [2][0]
e poi [3][0]
e poi di nuovo .... [0][0]
spinta non sembra al lavoro
Qualche idea?
Soluzione
Mentre gli altri hanno detto, è length
, non lenght
.
Ma nessuno sembra avere affrontato la seconda parte della tua domanda, quindi:
Non è necessario push
per scorrere i valori. Tutto ciò che serve è un indice:
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++];
}
In alternativa, se si vuole, si può semplicemente mettere l'indice direttamente sull'oggetto array, poiché JavaScript oggetti array possono avere arbitrarie proprietà non-elemento e che aiuta a mantenere i simboli insieme:
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 realtà, si può anche fare la parte funzione della matrice:
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();
Se fare la matrice stessa del contenitore di queste proprietà in più ti dà fastidio (dà fastidio alcune persone), avvolgere il tutto in un oggetto:
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
Altri suggerimenti
controllare l'ortografia ..
fondetcaption.length
Hai provato:
var howmany = fondetcaption.length;
Il tuo post originale ha un errore di ortografia, Lunghezza deve essere lunghezza .
Prova length
.
alert(fondetcaption.length);
Non so cosa c'è di sbagliato in quanto funziona bene qui ..
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
alert(fondetcaption.length);
Forse fondetcaption.lenght
è in realtà copiato dal codice, e questa convenzione farro in modo non corretto.
Ciò restituisce la lunghezza dell'array: fondetcaption.length
Hai un errore di battitura nel codice. Dovrebbe essere var howmany = fondetcaption.length
, e non ... fondetcaption.lenght
.
Vi suggerisco di utilizzare strumenti come Firebug di, Chrome, o Console di Safari. Può essere molto utile, soprattutto con i problemi come questi. I copia-incollato il codice, provato, e funziona come fascino. Con il citato errore di battitura fisso, ovviamente.
Saluti.