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

97
Views
remove the second occurrence of array object with same name

I have this array (java script array of objects)

users=[{name:'arrow',age:50,id:444}
       {name:'bow',age:66,id:884}
       {name:'arrow',age:30,id:99},
       {name:'apple',age:50,id:999}
       {name:'bow',age:50,id:9669}]

I want to remove second occurrence of same name , in this case , I want to remove {name:'arrow',age:30,id:99} and {name:'bow',age:50,id:9669} and retain first occurrences{name:'arrow',age:50,id:444} and {name:'bow',age:66,id:884}

Resulting array should be :

users=     [{name:'arrow',age:50,id:444}
           {name:'bow',age:66,id:884},
           {name:'apple',age:50,id:999}]
       
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const users = [
    { name: 'arrow', age: 50, id: 444 },
    { name: 'bow', age: 66, id: 884 },
    { name: 'arrow', age: 30, id: 99 },
    { name: 'apple', age: 50, id: 999 },
    { name: 'bow', age: 50, id: 9669 }
]

const uniqueUsers = users.reduce((acc, user) => {
    if (!acc.find(u => u.name === user.name)) {
        acc.push(user)
    }
    return acc
}, [])
about 4 years ago · Juan Pablo Isaza Report

0

I'd go with the approach of array.filter:

function removeDuplicateKeyFromArray(arrayOfObjects,keyName){
  keyHolder = {}
  arrayOfObjects.filter((obj)=>{
      if(keyHolder[obj.keyName]){
         return false 
       }
      keyHolder[obj.keyName] = 1 //or true
      return true
    })
}
about 4 years ago · Juan Pablo Isaza Report

0

I would create 2 for-loops, to filter out any duplicates. here's my code:

let users = [{name:'arrow',age:50,id:444},
  {name:'bow',age:66,id:884},
  {name:'arrow',age:30,id:99},
  {name:'apple',age: 50,id: 990},
  {name:'bow',age: 50,id: 9669}]

for (let i = 0; i < users.length; i++) {
  for(let x = 0; i < users.length; i++) {
    if(users[i].name == users[x].name) {
      users.splice(users[x], 1)
    }
  }
}
about 4 years ago · Juan Pablo Isaza 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!