I'm looking for an efficient way to return unique values in objects inside an array. For example the next object:
{
"products": [{
"id": 1,
"category": "test1",
"tags": {
"option": ["A", "B"]
}
}, {
"id": 2,
"category": "test2",
"tags": {
"option": ["B"],
"type": ["A", "B", "C"]
}
}, {
"id": 3,
"category": "test1",
"tags": {
"type": ["A"]
}
}, {
"id": 4,
"category": "test2",
"tags": {
"option": ["B", "C"],
"type": ["A", "C"]
}
}]
}
What I want to return is the following:
{"option": [ "A", "B", "C" ] },{"type": ["A", "B", "C"] }
So I want for each item inside the tags object a new object. After that, I want an array with all unique values over all the products.
I do somewhat the same with another function:
Array.from(new Set(data.map(p => { return p.category; })))
This is a level higher which makes it easier. Can someone push me in the right direction?
p.category? There is no mention of it in your sample data.data.jsonfile, it's not an object.