7

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
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • Id advise you to use Firefox and download the Firebug addon. I used to have q's like this, till I got the addon. – Robert Mailloux Mar 12 '13 at 05:41
  • Or use [Google Chrome browser](https://www.google.com/intl/en/chrome/browser/), has great developer tools. – Amy Mar 12 '13 at 05:47
  • I didn't downvote, but maybe it's because the question would be easy to answer with a small amount of research and/or reading. Sometimes it's tempting to downvote legitimate questions when it seems like the OP was stuck on something for 3 minutes before asking the question. – jahroy Mar 12 '13 at 05:58
  • 1
    `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 朕會功夫 Mar 12 '13 at 06:02
  • In your example, `[0, 0, 3, 5]` will turn into `0,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 朕會功夫 Mar 12 '13 at 06:11
  • Possible duplicate: http://stackoverflow.com/questions/2509432/how-to-get-array-keys-in-javascript – Anderson Green Mar 12 '13 at 17:32

5 Answers5

11

Actually 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/

Community
  • 1
  • 1
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
3

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/

Latikov Dmitry
  • 956
  • 5
  • 8
  • I see in FF/Chrome: `Hello World1 Hello World {"0,0,3,5":"Hello World","0,0,3":"Hello World1"}` – Latikov Dmitry Mar 12 '13 at 05:54
  • This is a good workaround, but I still want to know how to print the defined coordinates of an array instead of an object. – Anderson Green Mar 12 '13 at 05:55
  • @AndersonGreen - An array is an object. See the answer by user960567 – jahroy Mar 12 '13 at 05:56
  • @jahroy I still don't understand why this only works when coordinates is initialized to `{}` instead of `new Array()`. It seems a bit surprising to me. – Anderson Green Mar 12 '13 at 06:23
  • Definition from [mdn](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array): The JavaScript Array global object is a constructor for arrays, which are high-level, list-like objects. I think Array don't work fine in this case due to the fact that Array requires numeric keys. – Latikov Dmitry Mar 12 '13 at 06:51
1
for (i=0;i<coordinates.length;i++)
{
document.write(coordinates[i] + "<br >");
}
Robert Mailloux
  • 397
  • 4
  • 14
1

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 >");
    }
}
  • I still have the same problem: `coordinates.join(",")` doesn't print anything in this case. http://jsfiddle.net/GwgDN/7/ – Anderson Green Mar 12 '13 at 05:47
  • Instead of printing the coordinates, it now prints a list of functions: http://jsfiddle.net/GwgDN/10/ – Anderson Green Mar 12 '13 at 05:52
  • do not use `for in` for iterating array values. use for(;;) or Array.forEach – rlemon Mar 12 '13 at 12:37
  • Array.forEach is ok but for(;;) is not because we can iterate if array's index is numeric in some cases where key is "string" type then we can't use for(;;) –  Mar 12 '13 at 12:39
0

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.

Udo Klein
  • 6,784
  • 1
  • 36
  • 61