...

View Full Version : What kind of array is this?



gamshaft
07-20-2011, 03:45 PM
What do you call this kind of javascript array?



var markers = [
{ lat: -33.85, lng: 151.05, name: "marker 1" },
{ lat: -33.90, lng: 151.10, name: "marker 2" },
{ lat: -33.95, lng: 151.15, name: "marker 3" },
{ lat: -33.85, lng: 151.15, name: "marker 4" }
];
markers[0].name= "wow I was able to do this";


I don't see any javascript documentation on this.

gamshaft
07-20-2011, 04:12 PM
I guess you call them :

One can use the object declaration literal to create objects that behave much like associative arrays in other languages:


dog = {"color":"brown", "size":"large"};
dog["color"]; // results in "brown"
dog.color; // also results in "brown"



One can use the object and array declaration literals to quickly create arrays that are associative, multidimensional, or both. (Technically, JavaScript does not support multidimensional arrays, but one can mimic them with arrays-of-arrays.)



cats = [{"color":"brown", "size":"large"},
{"color":"black", "size":"small"}];
cats[0]["size"]; // results in "large"

dogs = {"rover":{"color":"brown", "size":"large"},
"spot":{"color":"black", "size":"small"}};
dogs["spot"]["size"]; // results in "small"
dogs.rover.color; // results in "brown"

jmrker
07-20-2011, 06:11 PM
What do you call this kind of javascript array?



var markers = [
{ lat: -33.85, lng: 151.05, name: "marker 1" },
{ lat: -33.90, lng: 151.10, name: "marker 2" },
{ lat: -33.95, lng: 151.15, name: "marker 3" },
{ lat: -33.85, lng: 151.15, name: "marker 4" }
];
markers[0].name= "wow I was able to do this";


I don't see any javascript documentation on this.

Expanding on your post. :D


<script type="text/javascript">
var markers = [
{ lat: -33.85, lng: 151.05, name: "marker 1" },
{ lat: -33.90, lng: 151.10, name: "marker 2" },
{ lat: -33.95, lng: 151.15, name: "marker 3" },
{ lat: -33.85, lng: 151.15, name: "marker 4" }
];
var str = '';
for (var i=0; i<markers.length; i++) {
str += markers[i].name+'\t'+markers[i].lat+'\t'+markers[i].lng+'\n';
}
markers[0].name= "wow I was able to do this";
str += '\n\n';
for (var i=0; i<markers.length; i++) {
str += markers[i].name+'\t'+markers[i].lat+'\t'+markers[i].lng+'\n';
}
alert(str);
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum