How to get array of unique values from arrays within an array of objects?

Jed

I have a given array of objects something like:

var items = [
{item: [{foo: 21, bar: 'a' }, {foo: 5,bar: 'e'},{foo: 167, bar: 'c'}]},
{item: [{foo: 42, bar: 'a' }, {foo: 45,bar: 'd'},{foo: 7, bar: 'c'}]},
{item: [{foo: 99, bar: 'b' }, {foo: 35,bar: 'c'},{foo: 22, bar: 'e'}]},
{item: [{foo: 31, bar: 'e' }, {foo: 22,bar: 'd'},{foo: 12, bar: 'a'}]}
]

and I would like to get a new array that returns the unique values of all the bar values, so it would look something like:

var uniqueBars = ['a','b','c','d','e'];

I have a solution for this by looping through all of the items, but I'm guessing there is a more efficient way to do this using ES6 features.

Is there a way to create the uniqueBars array above using ES6 features?

Nina Scholz

You could supply a path of keys and get the values for a Set.

const
    getValues = (data, [key, ...keys]) => data.flatMap(o => keys.length
        ? getValues(o[key], keys)
        : o[key]
    ),
    items = [{ item: [{ foo: 21, bar: 'a' }, { foo: 5, bar: 'e' }, { foo: 167, bar: 'c' }] }, { item: [{ foo: 42, bar: 'a' }, { foo: 45, bar: 'd' }, { foo: 7, bar: 'c' }] }, { item: [{ foo: 99, bar: 'b' }, { foo: 35, bar: 'c' }, { foo: 22, bar: 'e' }] }, { item: [{ foo: 31, bar: 'e' }, { foo: 22, bar: 'd' }, { foo: 12, bar: 'a' }] }],
    keys = ['item', 'bar'],
    unique = [...new Set(getValues(items, keys))];

console.log(...unique);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get objects with unique values for specific keys from array of objects?

How to get the combination of array values from nested arrays in an array of objects

How to get all values from an array of objects with nested arrays of objects?

Get unique values from array within a range?

How to get unique values from an array of JS objects?

how to get unique values from array of objects with addition of numbers

Get unique values from an array of objects

Get unique values from array of objects

How to get array of objects with unique values?

How to get unique an array of objects back from a complex an array of objects?

How to get an array of object values embedded within an array of objects

JavaScript How to get values from an array of objects into array of arrays so that each sub-array groups values from like keys?

How to get values from a hash within an array

How to get unique values from object array

How to get a the sum of multiple arrays within an array of objects?

Get the unique values from two arrays and put them in another array

How to get all specific values from an array of objects with nested arrays of objects?

How to get an array of unique properties from an array of complex objects

How to get values from one array of objects into another array of objects

Get unique values corresponding to the keys from an array of objects in JavaScript

How to get values from array of objects in javascript

Reactjs: How to get values from array of objects

How to merge all arrays of an object array to get unique values?

How to get unique objects from array of objects by all properties?

Get unique strings from array of array of objects

How can I get keys with unique values in an array of objects? (JavaScript)

How to get all unique objects (with two values) in an array?

How to output unique values from arrays without array functions?

Javascript: How to get a unique list of values assigned to the same property from an array of objects?