0

So I am playing around with the d3 examples from a course on safari (Rapid D3) and there is this code in there to build up a map for a option element:

  if (teams.indexOf(row.TeamID) < 0) {
    teams.push(row.TeamID);
    teams[row.TeamID] = row.Team;
  }

And row.TeamID is a string.

When this runs over a few teams I see the following in the debugger: debugger

What am I seeing here? I thought there were no associative arrays in javascript?

dashambles
  • 529
  • 8
  • 22

1 Answers1

0

There are no associative arrays per se (at least there used not to be), but objects are very often used as such, as they have properties (though there are issues with it due to inheritance).

Arrays are just a type of object, and like all objects, have properties.

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • I am aware of the first part of course (Objects as associative arrays) and vaguely of the second part (arrays with properties), but hadn't seen the use case before: in another place he is iterating over the array and using the value (i.e. mansfield-town) to fetch the name as a property teams["mansfield-town"] which pretty much simulates associative arrays. – dashambles Dec 12 '15 at 01:21
  • What does "at least there used not to be" mean? What are the "issues with it due to inheritance"? –  Dec 12 '15 at 05:07
  • @torazaburo, ES6 defines the `Map` object (and others), which are actual associate arrays. The issues with using objects as associative arrays is that when you try to walk through their properties with `for (... in ...)`, you'll also get anything that was set by the prototype chain, which may give surprising results if you don't check with `hasOwnProperty`. – jcaron Dec 12 '15 at 09:11