0

How to access nested array of all elements of an array in ES6? Eg

source = [
    {
       id: '1',
       list: ['a1','a2','a3']  
    },
   {
       id: '2',
       list: ['b1','b2']  
   }
]

i want an output ['a1','a2','a3','b1','b2]

i'm doing this but its returning array with length 1, 0th element ['a1','a2','a3','b1','b2].

source.map(x=>x.list).map(x=>x);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Huzan Toorkey
  • 185
  • 1
  • 9
  • 4
    Use `Array.flatMap()` instead - `source.flatMap(x=>x.list)`. – Ori Drori May 20 '21 at 10:21
  • 1
    Does this answer your question? [get a list of values from a specific js object key](https://stackoverflow.com/questions/67598998/get-a-list-of-values-from-a-specific-js-object-key) – ulou May 20 '21 at 10:23

0 Answers0