I'm trying to obtain a list of all elements that are in a JavaScript array, but I've noticed that using array.toString
does not always show all the contents of the array, even when some elements of the array have been initialized. Is there any way to print each element of an array in JavaScript, along with the corresponding coordinates for each element? I want to find a way to print a list of all coordinates that have been defined in the array, along with the corresponding values for each coordinate.
/
var coordinates = [];
coordinates[[0, 0, 3, 5]] = "Hello World";
coordinates[[0, 0, 3]] = "Hello World1";
console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(coordinates.toString()); //this doesn't print anything at all, despite the fact that some elements in this array are defined
I'm trying to obtain a list of all elements that are in a JavaScript array, but I've noticed that using array.toString
does not always show all the contents of the array, even when some elements of the array have been initialized. Is there any way to print each element of an array in JavaScript, along with the corresponding coordinates for each element? I want to find a way to print a list of all coordinates that have been defined in the array, along with the corresponding values for each coordinate.
http://jsfiddle.net/GwgDN/3/
var coordinates = [];
coordinates[[0, 0, 3, 5]] = "Hello World";
coordinates[[0, 0, 3]] = "Hello World1";
console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(coordinates.toString()); //this doesn't print anything at all, despite the fact that some elements in this array are defined
Share
Improve this question
edited Mar 12, 2013 at 6:21
Anderson Green
asked Mar 12, 2013 at 5:40
Anderson GreenAnderson Green
31.8k69 gold badges209 silver badges339 bronze badges
6
|
Show 1 more comment
5 Answers
Reset to default 11Actually when you use coordinates[[0, 0, 3]] then this means coordinates object with [0, 0, 3] as key. It will not push an element to array but append a property to the object. So use this line which loop through objects. See this for other ways to loop through object properties,
Object.keys(coordinates).forEach(function(key) {
console.log(key, coordinates[key]);
});
http://jsfiddle.net/GwgDN/17/
Use type 'object' instead 'array' for coordinates
var coordinates = {};
coordinates[[0, 0, 3, 5]] = "Hello World";
coordinates[[0, 0, 3]] = "Hello World1";
console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(JSON.stringify(coordinates));
http://jsfiddle.net/5eeHy/
for (i=0;i<coordinates.length;i++)
{
document.write(coordinates[i] + "<br >");
}
use join function to get all elements of array.use following code
for (var i in coordinates)
{
if( typeof coordinates[i] == 'string' ){
console.log( coordinates[i] + "<br >");
}
}
Look at coordinates in the debugger and you will see that you have set the properties [0,0,3,5] and [0,0,3] of the object coordinates. That is although coordinates is an array you are not using it as an array.
despite the fact that some elements in this array are defined
Arrays only take in integer indexes:[][0] = 1
,[]["string"] = 1
won't work. – Derek 朕會功夫 Commented Mar 12, 2013 at 6:02[0, 0, 3, 5]
will turn into0,0,3,5
, creating an Object:{"0,0,3,5": "Hello World"}
. Since there is no item in your Array,coordinates,toString()
will output""
. – Derek 朕會功夫 Commented Mar 12, 2013 at 6:11