-2

Obviously, this is not allowed in javascript:

for (var i=0; i<data.length; i++){
    array[i].push(data[i].name);
}

Does anyone know of a workaround that does the same thing?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Alexey Ayzin
  • 209
  • 2
  • 21

2 Answers2

1

You can do

for (var i=0; i<data.length; i++){
    array[i] = [];
    array[i].push(data[i].name);
}

https://jsfiddle.net/zv0xfzzs/

Maantje
  • 1,781
  • 2
  • 20
  • 33
0

but Alexey Ayzin, shurely this is the better alternative - create a single array, push the desired values into it and then access it via normal means::

arrayTest = [];
for (var i=0; i<data.length; i++){
      arrayTest.push(data[i].name);
}

This will give arrayTest all names, and can be accessed as:

var testName=arrayTest[3];

Doing it the other way will give you lots of unnamed arrays - how are they to be used?

gavgrif
  • 15,194
  • 2
  • 25
  • 27