Given the following array:
foos = [
{
id: 0,
bar: ['a','b','c']
},
{
id: 1,
bar: ['a','b','d']
},
{
id: 2,
bar: ['a','c']
},
]
Using reduce, how can I achieve the following?:
bars == ['a','b','c','d']
I've tried:
foo.reduce((bars, foo) => bars.add(foo.bar), new Set())
But it results in a set of objects:
Set { {0: 'a', 1: 'b', 2: 'c'}, {0: 'a', 1: 'b', 2: 'd'}{0: 'a', 1: 'c'}}
And:
foos.reduce((bars, foo) => foo.bar.forEach(bar => bars.add(bar)), new Set())
But the forEach has no access to the bars set.
foo.barand add them individually tobars.add(). And return thebarsset from thereducenew Set( foos.flatMap(f => f.bar) )