I've got some data like this:
const items = [
{ _id: '1', reference: ['abc'] },
{ _id: '2', reference: ['def'] },
{ _id: '3', reference: ['abc'] }
]
The length of items is always different.
Now I need to get all unique reference strings in a single array. So the result should be
['abc', 'def']
as 'abc' is a duplicate.
I tried to use a forEach() loop:
const references = []
items.forEach(i => {
references.concat(i.reference)
})
console.log(references)
But references just gets an empty array result. Also with that I did not take care of duplicates...
I would like to use an ES6 pattern. With that I know I could use something like this:
const array1 = ['abc']
const array2 = ['def']
const array3 = Array.from(new Set(array1.concat(array2)))
But how can I do this using a loop to get every reference array of each item object - even if I do not know how many objects are in the item array?