Home » Javascript » merging specific object values from array into separated array

merging specific object values from array into separated array

Posted by: admin June 30, 2018 Leave a comment

Questions:

I have this array of persons

personsList = [
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C', 'D']}
]

in every object inside there more details beside cities but I want to make an array with all the cities inside – unique. so the result will be
['A', 'B', 'C', 'D']

I tried doing something like

const allCities = personsList.map(p => p.cities)

but then I got an array of arrays, what is the best approach for this one ? thanks.

Answers: