Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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)

about 4 years ago · Santiago Trujillo Report

0

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)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!