I can\'t find a similar question and I\'m a bit stuck. I have the following JSON array:
[
{
\"Name\": \"element1\",
\"Attributes\": [\"1\
Building on top of @dfsq answer, you could replace the two map
and reduce
with a single flatMap
var result = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
// map & flatten to [ "1", "2", "1", "3" ]
.flatMap(item => item.Attributes)
// filter unique [ "1", "2", "3" ]
.filter((item, i, arr) => arr.indexOf(item) === i)
console.log(result)