orbnexus
orbnexus

Reputation: 807

Remove duplicates within the map function

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' ]

Upvotes: 1

Views: 3093

Answers (2)

cmgchess
cmgchess

Reputation: 10297

you can achieve this using flatMap to get all name objects and then map to extract the name field. After that a Set to dedupe. [...new Set(..)] converts the Set back to an array

const results = [{
    "data": {
        "labels": {
            "results": [{
                "name": "tom"
            }, {
                "name": "another name"
            }]
        }
    }
}, {
    "data": {
        "labels": {
            "results": [{
                "name": "jerry"
            }]
        }
    }
}, {
    "data": {
        "labels": {
            "results": [{
                "name": "tom"
            }]
        }
    }
}]

const res = [...new Set(results.flatMap(({data:{labels: {results}}}) => results).map(({name}) => name))]

//alternative
//const res = [...new Set(results.map(({data:{labels: {results}}}) => results).flat().map(({name}) => name))]

console.log(res)

Upvotes: 1

IT goldman
IT goldman

Reputation: 19493

I would use reduce since it is perfect for iterating an array with an accumulative value. In this case, an object.

const x = [{
    "data": {
      "results": [{
        "name": "tom"
      }]
    }
  },
  {
    "data": {
      "results": [{
        "name": "jerry"
      }]
    }
  },
  {
    "data": {
      "results": [{
        "name": "tom"
      }]
    }
  }
]

var result = Object.values(x.reduce(function(acc, item) {
  var name = item.data.results[0].name
  // acc[name] = item;
  acc[name] = name;
  return acc;
}, {}))

console.log(result)

Upvotes: 2

Related Questions