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>
toni
  • 443
  • 5
  • 14
  • It's because you call ["data"] = new Object(); . You need ["data"] = new Array(); or just: ["data"] = []; Otherwise it's an object. –  Apr 30 '12 at 14:11
  • `Mitarbeiter[0]["data"] = new Object();` -- You mean `new Array()`? – Brad Christie Apr 30 '12 at 14:11
  • @Brad no, he meant `... = [];` – Alnitak Apr 30 '12 at 14:17
  • @Alnitak: Same difference. `new Array()` is synonymous with `[]`. – Brad Christie Apr 30 '12 at 14:25
  • @BradChristie and nowadays considered the incorrect way of doing it. Will post reference when I find it. – Alnitak Apr 30 '12 at 14:26
  • @Alnitak: I trust [MDN's Docs on Arrays](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array) any day over some guy's blog, TBH. (That isn't to say where you saw it isn't reputable, but I trust MDN completely). Also, `[]` is to Arrays what `$.fn.validator && $('form.validate').validate();` is to short-circuiting logic. It still translates to an Array object (and therefore implements `Array.prototype` methods.) – Brad Christie Apr 30 '12 at 14:31
  • Well, according to Crockford: "Typed Wrappers JavaScript has a set of typed wrappers. For example: new Boolean( false) produces an object that has a valueOf method that returns the wrapped value. This turns out to be completely unnecessary and occasionally confusing. Don't use new Boolean or new Number or new String. Also avoid new Object and new Array. Use {} and [] instead." Of course, unnecessary and confusing doesn't mean it doesn't work. YMMV. – aquinas Apr 30 '12 at 16:56

3 Answers3

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

Should be

Mitarbeiter[0]["data"] = new Array();
aquinas
  • 23,318
  • 5
  • 58
  • 81
  • No @Alnitak it *should* be: `mitarbeiter[0]["data"] = [];` because mitarbeiter shouldn't be using a capital letter. :) – aquinas Apr 30 '12 at 14:51
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.

Guss
  • 30,470
  • 17
  • 104
  • 128
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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495