0

I´m trying to output the array fields of an array with ist in an Array (More dimensional array). It seems .length does not work in the second array.

Thanks for help!

cheers, toni

<html><head><title>Test</title>
</head><body>
<script type="text/javascript">
var Mitarbeiter = new Array();

Mitarbeiter[0] = new Object();
Mitarbeiter[0]["Name"] = "Hotels";
Mitarbeiter[0]["data"] = new Object();
Mitarbeiter[0]["data"][0] = "Ort 1";
Mitarbeiter[0]["data"][1] = "Ort 2";

Mitarbeiter[1] = new Object();
Mitarbeiter[1]["Name"] = "Restaurants";
Mitarbeiter[1]["data"] = new Object();
Mitarbeiter[1]["data"][0] = "Ort 2";
Mitarbeiter[1]["data"][1] = "Ort 4";



for (var i = 0; i < Mitarbeiter.length; i++) {

document.write("<b>" + i + " : " + Mitarbeiter[i]["Name"] + "</b><br />");

//works
alert (Mitarbeiter[0]["data"][i]);


// works not
for (var f = 0; f < Mitarbeiter[i]["data"].length; f++){
document.write("<br/>&nbsp;" + Mitarbeiter[i]["data"][f]);
}
}
</script>
</body>
</html>
7
  • It's because you call ["data"] = new Object(); . You need ["data"] = new Array(); or just: ["data"] = []; Otherwise it's an object. Commented Apr 30, 2012 at 14:11
  • Mitarbeiter[0]["data"] = new Object(); -- You mean new Array()? Commented Apr 30, 2012 at 14:11
  • @Brad no, he meant ... = []; Commented Apr 30, 2012 at 14:17
  • @Alnitak: Same difference. new Array() is synonymous with []. Commented Apr 30, 2012 at 14:25
  • @BradChristie and nowadays considered the incorrect way of doing it. Will post reference when I find it. Commented Apr 30, 2012 at 14:26

3 Answers 3

2
Mitarbeiter[0]["data"] = new Object();

Should be

Mitarbeiter[0]["data"] = new Array();
1
  • No @Alnitak it should be: mitarbeiter[0]["data"] = []; because mitarbeiter shouldn't be using a capital letter. :) Commented Apr 30, 2012 at 14:51
0

You can create the whole thing in one go using an "object literal":

var Mitarbeiter = [ {
    Name: 'Hotels',
    data: [ 'Ort 1', 'Ort 2' ]
  }, {
    Name: 'Restaurants',
    data: [ 'Ord 2', 'Ort 4' ]
  }
];

Note how the braces indicate the type of object - [ ... ] for a linear array, { ... } for a "normal" Javascript object.

Your data: fields use numeric indices and so should be arrays rather than objects, so use [ ... ] syntax.

0

Your "data" fields are Objects (i.e. property lists or dictionaries) and not arrays, and as such have not length property. The construct

var data = new Object();
data[0] = "Ort 1";
data[1] = "Ort 2";

is identical to

var data = { 0: "Ort 1", 1: "Ort 2" };

Where obviously there is no length to be found.

I would have written the whole thing like this:

var Mitarbeiter = new Array();

Mitarbeiter.push( { 
    Name: "Hotels",
    data: [ "Ort 1", "Ort 2" ]
});

Mitarbeiter.push( {
    Name: "Restaurants",
    data: [ "Ort 2", "Ort 4"] 
});

I think its more concise and easier to understand.

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.