Pregunta de matriz simple
-
25-10-2019 - |
Pregunta
Tengo una matriz de 2 dimensiones:
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
Esa matriz puede tener 4, 7, 10, cualquier número de valor en ...
Me gusta saber cuántos par de valor tengo (debería devolver 4 en este caso)
var howmany = fondetcaption.lenght; // doesn't work !
Y después de eso ... mostraré fondetcaption[0][0]
(el primer fondo) Y después de eso, haga clic en un botón, me gusta mostrar el siguiente: [1][0]
Y luego el siguiente [2][0]
y entonces [3][0]
y entonces [0][0]
De nuevo ... el empuje no parece funcionar.
¿Alguna idea?
Solución
Como los demás han dicho, es length
, no lenght
.
Pero nadie parece haber abordado la segunda parte de su pregunta, así que:
No necesitas push
para recorrer los valores. Todo lo que necesitas es un índice:
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++];
}
O, si lo desea, simplemente puede colocar el índice directamente en el objeto de matriz, ya que los objetos de matriz de JavaScript pueden tener propiedades arbitrarias no elementales y eso ayuda a mantener juntos los símbolos:
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++];
}
De hecho, incluso puede hacer que la función sea parte de la matriz:
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();
Si hacer de la matriz en sí el contenedor de esas propiedades adicionales le molesta (molesta a algunas personas), envuelva todo en un objeto:
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
Otros consejos
corrige tu ortografía..
fondetcaption.length
Has probado:
var howmany = fondetcaption.length;
Tu publicación original tiene un error de ortografía, largo necesita ser longitud.
Probar length
.
alert(fondetcaption.length);
No estoy seguro de qué está mal, ya que funciona bien aquí.
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
alert(fondetcaption.length);
Quizás fondetcaption.lenght
en realidad se copia del código y, por lo tanto, se escribe de manera incorrecta.
Esto devuelve la longitud de la matriz: fondetCaption.length
Tienes un error tipográfico en código. Debería ser var howmany = fondetcaption.length
, y no ... fondetcaption.lenght
.
Le sugiero que use herramientas como la consola de Firebug, Chrome o Safari. Puede ser muy útil, especialmente con problemas como estos. Copié un código, lo probé y funciona como Charm. Con el error tipográfico mencionado fijo, por supuesto.
Salud.