Question: 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?

Answer: 
If you want to use Sets, you can use `Set.add`, and [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax):

```js
const items = [
  { _id: '1', reference: ['abc'] },
  { _id: '2', reference: ['def'] },
  { _id: '3', reference: ['abc'] }
]

var references = new Set();
items.forEach(i => references.add(...i.reference))
console.log(Array.from(references))
```
This is similar to other solutions here, but creates a reusable function:

```js
const uniqRefs = 
  items => [...items.reduce((s, i) => s.add(...i.reference), new Set())]

const items = [{"_id": "1", "reference": ["abc"]}, {"_id": "2", "reference": ["def"]}, {"_id": "3", "reference": ["abc"]}]
console.log(uniqRefs(items))
```