Get a list of all elements in a JavaScript array

I am trying to get a list of all the elements that are in the JavaScript array, but I noticed that using array.toStringdoes not always display the entire contents of the array, even if some elements of the array have been initialized. Is there a 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 the coordinates that were 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
+5
source share
5 answers

, [[0, 0, 3]], , [0, 0, 3]. , . , . ,

Object.keys(coordinates).forEach(function(key) {
    console.log(key, coordinates[key]);
});

http://jsfiddle.net/GwgDN/17/

+6

'object' 'array'

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/

+3
for (i=0;i<coordinates.length;i++)
{
document.write(coordinates[i] + "<br >");
}
+1

, array.use

for (var i in coordinates)
{
    if( typeof coordinates[i] == 'string' ){
        console.log( coordinates[i] + "<br >");
    }
}
+1

, , [0,0,3,5] [0,0,3] . - , .

0

All Articles