0

I don't understand why is array so strange when I add element by key. For example:

var test = new Array();
test['a'] = 'aaa';
test['b'] = 'bbb';

test.length == 0; // true. Why?

And when I run:

test.map(function(el){
   console.log(el);
});
// nothing print. Why?

When I add element with push method everything works OK. Thanks

John
  • 2,494
  • 5
  • 21
  • 24

1 Answers1

2

Arrays in javascript are not associative, you can't set keys like that i.e.

var test = [];

test.push('aaa');
test.push('bbb');

// OR

test[0] = 'aaa';
test[1] = 'bbb';

test.length = 2
atmd
  • 7,430
  • 2
  • 33
  • 64