jamesRH
jamesRH

Reputation: 440

Javascript: How is an array with keys possible?

I am trying to understand how force works in d3 and while testing code in Chromes javascript console, I came across arrays like such:

[14, 7, index: 19, weight: 4, x: 823.4318332231393, y: 87.20455959056517, 
px: 823.4318332231393…]

Since I am not aware of Arrays being able to have anything other than numeric keys, I am a little confused. How is this possible? Array.isArray is true.

Upvotes: 1

Views: 71

Answers (1)

Mark C.
Mark C.

Reputation: 6460

It looks to me like perhaps you've omitted some code, or perhaps have the incorrect snippet.

The closest acceptable format to what is in your question would be if they were anonymous objects within the array. Which would make sense, because d3 needs to know those specific properties (x, y, etc) to draw the elements.

My best assumption is that this is the actual array:

var arr = [14, 7, {index : 19 }, {weight: 4} , {x: 823.4318332231393}, {y: 87.20455959056517}, {px: 823.4318332231393}]

Which yields true for Array.isArray(arr);

Upvotes: 4

Related Questions