0

Ive got an array of objects. Inside each object there is another array. I want to extract distinct values from these arrays.

var data = [
            {categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
            {categorie: 'Stuff', type: 'Two', designs: ['C']},
            {categorie: 'Stuff', type: 'One', designs: ['D']},
            {categorie: 'Other', type: 'Three', designs: ['C', 'D']}
        ];
console.log([...new Set(data.map(x => x.categorie))]);
console.log([...new Set(data.map(x => x.type))]);

//expected output for designs ['A','B','C','D']

3
  • 1
    like console.log([...new Set(data.flatMap(x => x.designs))]);? Commented Jul 9, 2019 at 8:10
  • "expected output for designs" - Why are you then returning x.categorie/x.type? Commented Jul 9, 2019 at 8:10
  • both other console.logs are just an example how i would return distincts from categorie and type. But dont know how to do it from designs. Commented Jul 9, 2019 at 8:11

4 Answers 4

4

You can use flatMap() instead of map()

var data = [
            {categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
            {categorie: 'Stuff', type: 'Two', designs: ['C']},
            {categorie: 'Stuff', type: 'One', designs: ['D']},
            {categorie: 'Other', type: 'Three', designs: ['C', 'D']}
        ];

console.log([...new Set(data.flatMap(x => x.designs))]);

//expected output for designs ['A','B','C','D']

If your browser doesn't support flatMap() then you can use concat() with spread operator.

var data = [
            {categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
            {categorie: 'Stuff', type: 'Two', designs: ['C']},
            {categorie: 'Stuff', type: 'One', designs: ['D']},
            {categorie: 'Other', type: 'Three', designs: ['C', 'D']}
        ];

console.log([...new Set([].concat(...data.map(x => x.designs)))]);

//expected output for designs ['A','B','C','D']

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

Comments

1

You need to take a set from the designs as well.

var data = [{ categorie: 'Stuff', type: 'One', designs: ['A', 'B'] }, { categorie: 'Stuff', type: 'Two', designs: ['C'] }, { categorie: 'Stuff', type: 'One', designs: ['D'] }, { categorie: 'Other', type: 'Three', designs: ['C', 'D'] }],
    unique = data.reduce((s, { designs }) => [...new Set([...s, ...designs])], []);

console.log(unique);

1 Comment

What about let res = [...new Set(data.reduce((a,{designs}) => [...a,...designs],[]))]. Wouldn't it be better to just flatten first and form a set once at the end, instead of forming a set on every reduction step?
0

A simple solution is this:

var data = [
            {categorie: 'Stuff', type: 'One', designs: ['A', 'B']},
            {categorie: 'Stuff', type: 'Two', designs: ['C']},
            {categorie: 'Stuff', type: 'One', designs: ['D']},
            {categorie: 'Other', type: 'Three', designs: ['C', 'D']}
        ];

var uniqueLetters = []

data.forEach(obj => {
  obj.designs.forEach(letter => {
    if (uniqueLetters.indexOf(letter) < 0) {
      uniqueLetters.push(letter)
    }
  })
})

console.log(uniqueLetters)

Comments

0

Try this.. Plain JavaScript solution.

var output = [];
for(var i=0; i<data.length; i++){
for(var j=0; j<data[i].designs.length; j++ ){
  if(output.indexOf(data[i].designs[j]) === -1) {
  output.push(data[i].designs[j])
  }
}
}
console.log(output)
//expected output for designs ['A','B','C','D']

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.