0

How do I split an array into individual array: [1,2,3,4,5] => [1][2][3][4][5]

The array of objects is gotten from a backend API

const arr = [
  {
    id: '1',
    name: 'Emmanuel',
    ...
  },
  {
    id: '1',
    name: 'Emmanuel',
    ...
  }
]

Doing arr.map(a => a.id) gave me [1,2,...], now how do I get individual array from the ids: [1][2]

Peoray
  • 1,705
  • 2
  • 12
  • 21
  • where is the target of single arrays? – Nina Scholz Apr 20 '20 at 19:56
  • I think you need to give more clarity to the question because the answer from @NinaScholz would seem correct currently. – Fasani Apr 20 '20 at 19:57
  • Does this answer your question? [How to split a long array into smaller arrays, with JavaScript](https://stackoverflow.com/questions/7273668/how-to-split-a-long-array-into-smaller-arrays-with-javascript). In this case, the size of the "smaller arrays" is 1. – Heretic Monkey Apr 20 '20 at 20:01
  • Or, if you want them as separate variables, [Unpacking array into separate variables in JavaScript](https://stackoverflow.com/q/3422458/215552) – Heretic Monkey Apr 20 '20 at 20:04

1 Answers1

3

Just wrap the return value in an array and take a destructuring assignment into own targets.

let [t1, t2, t3, t4, t5] = arr.map(a => [a.id]);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392