2010-08-24 21 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'); 
    } 
}); 

tutto funziona, eccetto che non arriva al documento.write. Qualcuno ha un'idea del perché?jquery.each array multidimensionale

+1

Quelli sono * * non ** Array che stai usando lì, ma Oggetti normali. Non usare 'new Array()' ma 'new Object()' (o semplicemente '{}' per un oggetto "vuoto"). – RoToRa

risposta

8

Si noti che la sostituzione Array() è la chiave qui con i missing ')'

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'); 
    }); 
}); 

e un altro modo di scrivere la stessa cosa: (piccolo tweek sulla scrittura per fare riferimento l'oggetto)

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); 
    }); 
}); 

E, infine, con la deprecata new Object() sostituzione:

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: divertirsi un po 'di spirito h l'array, e perché si potrebbe avere le corde della dichiarazione oggetto come lo avete:

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); 
    }); 
}); 

l'uscita del l'ultimo è: "test Howdy: Dati: qualche altro testo"

+1

Beh, non è VERAMENTE un array, ma comunque divertente :) arguzia h il tuo oggetto array1! –

+0

Ti stai divertendo troppo con questo. :) –

+1

Non riesco a postare altri modi "divertenti" per dichiarare questo set di oggetti :) –

1

La document.write non funziona come hai un errore di sintassi, in modo che il flusso di codice non viene mai ad esso - è necessario un altro supporto al termine del vostro each, cioè

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

Se Farò qualsiasi lavoro non banale con javascript, ti consiglio vivamente di usare Firefox con Firebug installato - la console evidenzia questi tipi di errori che altrimenti fallirebbero senza che te ne rendessi conto, portandoti a credere che tutto funzionasse correttamente .

+1

Sì sulla piattaforma o su un altro browser, ma almeno acquisire i dettagli dell'errore dettagli della pagina Web come ho fatto facendo clic sulla notazione degli errori in una finestra di IE facendo doppio clic sulla barra di stato: User Agent: Mozilla/4.0 (compatibile; 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) Data/ora: mar 24 ago 2010 15:28:09 UTC Messaggio: Previsto ')' –

+1

che non ha ancora colpito il documento.write come vedi qui: jsfiddle .net/yPYWd –

1

Si perde ) nel secondo each.

+1

che ancora non ha colpito il documento.write come vedi qui: http://jsfiddle.net/yPYWd/ –