I want to remove duplicates within the map function and I have looked through the different posts on the internet but I am still not finding the solution.
The JSON:
"results": [
{
"data": {
"labels": {
"results": [
{
"name": "tom"
}
]
}
}
},
{
"data": {
"labels": {
"results": [
{
"name": "jerry"
}
]
}
}
},
{
"data": {
"labels": {
"results": [
{
"name": "tom"
}
]
}
}
}
]
The code:
obj.results.map((items) => {
if (items.data.labels.results.length) {
items.data.labels.results.map((result) => {
console.log(result.name);
});
}
});
Result
tom
jerry
tom
Expected Result
tom
jerry
Tried this solution but didn't work
obj.results.map((items) => {
if (items.data.label.results.length) {
items.data.label.results.map((result) => {
console.log(Array.from(new Set(result.name)));
});
}
});
Result from above code
[ 't', 'o', 'm' ]
[ 'j', 'e', 'r', 'r', 'y' ]
[ 't', 'o', 'm' ]