2010-08-24 9 views
9
var array1 = {}; 

array1['one'] = new Array(); 
array1['one']['data'] = 'some text'; 
array1['one']['two'] = new Array(); 
array1['one']['two']['three'] = new Array(); 
array1['one']['two']['three']['data'] = 'some other text'; 

$.each(array1, function(key1, value1){ 
    $.each(value1['two']['three'], function(key1, value1){ 
     document.write('test'); 
    } 
}); 

todo funciona, excepto que no llega al documento.write. Alguien una idea por qué?matriz multidimensional jquery.each

+1

Esos son * * no ** Matrices que está utilizando allí, pero objetos normales. No use 'new Array()' pero 'new Object()' (o simplemente '{}' para un objeto "empty"). – RoToRa

Respuesta

8

Tenga en cuenta que la sustitución Array() es clave en este caso con los desaparecidos ')'

var array1 = {}; 

array1['one'] = new Object(); 
array1['one']['data'] = 'some text'; 
array1['one']['two'] = new Object(); 
array1['one']['two']['three'] = new Object(); 
array1['one']['two']['three']['data'] = 'some other text'; 

$.each(array1, function(key1, value1) { 
    $.each(value1['two']['three'], function(key1, value1) { 
     document.write('test'); 
    }); 
}); 

y Otra manera de escribir lo mismo: (pequeña tweek en la escritura para hacer referencia a su objeto)

var array1 = {}; 

array1.one = new Object(); 
array1.one.data = 'some text'; 
array1.one.two = new Object(); 
array1.one.two.three = new Object(); 
array1.one.two.three.data = 'some other text'; 


$.each(array1, function(key1, value1) { 
    $.each(value1['two']['three'], function(key1, value1) { 
     document.write('test' + array1.one.data); 
    }); 
}); 

Y, por último, con el desuso new Object() de reemplazo:

var array1 = {}; 

array1['one'] = {} 
array1['one']['data'] = 'some text'; 
array1['one']['two'] = {}; 
array1['one']['two']['three'] = {}; 
array1['one']['two']['three']['data'] = 'some other text'; 

$.each(array1, function(key1, value1) { 
    $.each(value1['two']['three'], function(key1, value1) { 
     document.write('test'); 
    }); 
}); 

EDIT: cierto ingenio divertido h la matriz, y por qué es posible que tenga las cuerdas en la declaración objeto como lo tienes:

var array1 = {}; 
var fun="four"; 
array1.one = {}; 
array1.one.data = 'some text'; 
array1.one.two = {}; 
array1.one.two.three = {}; 
array1.one.two.three.data = 'some other text'; 
array1.one.two[fun] = {}; 
array1.one.two[fun].data=' howdy'; 

$.each(array1, function(key1, value1) { 
    $.each(value1.two.three, function(key1, value1) { 
     document.write('test'+array1.one.two[fun].data+ ":"+key1+":"+value1); 
    }); 
}); 

la salida del último es: "prueba Howdy: los datos: un texto diferente"

+1

Bueno, no es REALMENTE una matriz, pero de todos modos es divertido :) ingenio h su objeto array1! –

+0

Te estás divirtiendo demasiado con esto. :) –

+1

Me resisto a publicar más formas "divertidas" de declarar este objeto :) –

1

El document.write no está funcionando como usted tiene un error de sintaxis, por lo que el flujo de código nunca llega a él - usted necesita otro soporte en el extremo de su each, es decir

$.each(array1, function(key1, value1){ 
    $.each(value1['two']['three'], function(key1, value1){ 
     document.write('test'); 
    }) 
}); 

Si Estará haciendo cualquier trabajo no trivial con javascript, recomiendo usar Firefox con Firebug instalado - su consola resalta este tipo de errores que de otra manera fallarían sin que usted se diera cuenta, haciéndolo creer que todo estaba funcionando bien .

+1

Sí en la plataforma u otro navegador, pero al menos capture los detalles del error Detalles de error de página web como hice haciendo clic en la notación de error en una ventana IE haciendo doble clic en la barra de estado: Usuario Agente: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; MDDR; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0) Marca de tiempo: Tue, 24 de agosto de 2010 15:28:09 UTC Mensaje: Se esperaba ')' –

+1

que aún no llega al documento.write como se ve aquí: jsfiddle .net/yPYWd –

1

Oliendo ) en el segundo each.

+1

que aún no llega al documento.write como se ve aquí: http://jsfiddle.net/yPYWd/ –

Cuestiones relacionadas