1

Can someone let me know how I can create a list of unique languages from an array inside another array.

This is the dataset...

const people = [
{
    //Other values, name date etc.
    languages: ["English", "Spanish"]
},{
    //Other values, name date etc.
    languages: ["English", "Mandarlin"]
},{
    //Other values, name date etc.
    languages: ["Japanese"]
},....

and here is as far as I've got....

const languagesOptions = this.props.data.map((item, index) => {
  new Map(
    ...item.languages.map(d => [d.languages])
  )
});

I can use the new Map function when it's not an array but can't get it to work with the languages data.

Thanks

1 Answer 1

1

You could map over it and use Set to remove dupes.

const people = [{
  languages: ["English", "Spanish"]
}, {
  languages: ["English", "Mandarlin"]
}, {
  languages: ["Japanese"]
}];

const languages = [...new Set(people.flatMap(({ languages }) => languages))];

console.log(languages);

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

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.