0

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.

1
  • p.cities is also array inner map function then use filter Commented May 23, 2018 at 7:14

5 Answers 5

2

You can try following using Set

var personsList = [
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C', 'D']}
]
 
// cities will have all the cities with duplicates
const allCities = personsList.reduce((a,c) => [...a, ...c.cities], []);
// create set from array - removes duplicate and then create array back from set
console.log(Array.from(new Set(allCities)));

Sign up to request clarification or add additional context in comments.

Comments

0

You can array#map all cities and then concat them into one array. Then using Set you can get the unique city.

const personsList = [ {cities: ['A', 'B', 'C']}, {cities: ['A', 'B', 'C']}, {cities: ['A', 'B', 'C']}, {cities: ['A', 'B', 'C', 'D']} ],
      uniqueCity = Array.from(new Set([].concat(...personsList.map(({cities}) => cities))));
console.log(uniqueCity);

3 Comments

Thanks, for the reply @Hassan, how do I run trim() on each city ?
Then you can use array#map. uniqueCity.map(city => city.trim());
but then I'll have duplicate between A and A in the final array
0

You could use directly a Set for getting unique values.

var personsList = [{ cities: ['A', 'B', 'C'] }, { cities: ['A', 'B', 'C'] }, { cities: ['A', 'B', 'C'] }, { cities: ['A', 'B', 'C', 'D'] }],
    unique = Array.from(
        personsList.reduce(
            (s, { cities }) => cities.reduce((t, c) => t.add(c), s),
            new Set
        )
    );

console.log(unique);

1 Comment

var isn't technically deprecated, but I definitely think it should be avoided in all modern JavaScript
0

I think this is the effect that you are looking for, by using set you guarantee yourself that there are only unique elements.

let personsList = [
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C']},
  {cities: ['A', 'B', 'C', 'D']}
]
const allCities = personsList.reduce((acc,e) => {
  e.cities.map(x=>{
  acc.add(x)
  })
  return acc
}, new Set())
console.log(Array.from(allCities))

Comments

0

const personsList = [
  { cities: ['A', 'B', 'C'] },
  { cities: ['A', 'B', 'C'] },
  { cities: ['A', 'B', 'C'] },
  { cities: ['A', 'B', 'C', 'D'] }
];
const allCities = [...new Set([].concat(...personsList.map(({ cities }) => cities)))];
console.log(allCities);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.