Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.each multidimensional array

Tags:

jquery

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

everything works, except it doesnt get to the document.write. Anyone an idea why?

like image 361
dazz Avatar asked Aug 24 '10 14:08

dazz


2 Answers

Note that the Array() replacement is key here with the 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');
    });
});

and Another way to write the same thing:(small tweek on the write to reference your object)

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

And finally, with the deprecated new Object() replacement:

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: some fun with your array, and why you MIGHT have the strings in the object declaration as you have it:

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

the output the the last is: "test howdy:data:some other text"

like image 132
Mark Schultheiss Avatar answered Sep 22 '22 09:09

Mark Schultheiss


The document.write isn't working as you've a syntax error, so the code flow never gets to it - you need another bracket at the end of your each, i.e.

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

If you're going to be doing any non-trivial work with javascript, I'd highly recommend that you use Firefox with Firebug installed - it's console highlights these kind of errors which would otherwise fail without you realising, leading you to believe everything was working ok.

like image 23
ConroyP Avatar answered Sep 19 '22 09:09

ConroyP