12
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?

1
  • 1
    Those are not Arrays you are using there, but normal Objects. Do no use new Array() but new Object() (or just {} for an "empty" object). Commented Aug 24, 2010 at 15:16

3 Answers 3

12

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"

Sign up to request clarification or add additional context in comments.

2 Comments

Well, it's not REALLY an array, but fun anyway :) with your array1 object!
I resist posting more "fun" ways to declare this object set :)
1

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.

2 Comments

Yes on the platform, or another browser, but at least capture the error details Webpage error details as I did by clicking the error notation in an IE window by double clicking it in the status bar: User Agent: 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) Timestamp: Tue, 24 Aug 2010 15:28:09 UTC Message: Expected ')'
that still does not hit the document.write as you see here: jsfiddle.net/yPYWd
1

You miss ) in the second each.

1 Comment

that still does not hit the document.write as you see here: jsfiddle.net/yPYWd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.