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

133
Views
how to remove first element of duplicate in an array of objects

pardon my English , i want to remove first element of an array of object if it occur twice

here is the array=[{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]

i want to remove first element because third element's id is same as first element

output should be array=[{id:23,value:35}, {id:34,value:28}]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This will give you an array with the last value for duplicated elements, in preserved order. If you want exactly the 2nd one you need an extra flag

array=[{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]




const obj = array.reduce ( (acc,cur,index) => {
  acc[cur.id] = {index:cur};
  return acc;
},{});

const output = Object.values(obj).sort( (a,b) => a.index - b.index).map( ({index:val}) => val )

console.log(output)

about 4 years ago · Juan Pablo Isaza Report

0

A solution using ES6

const array = [
  { id: 34, value: 45 },
  { id: 23, value: 35 },
  { id: 34, value: 28 },
];

const convertToArray = array.map(({ id, value }) => [id, value]);
const convertToSet = Object.fromEntries(convertToArray);
const revertAsCleanedArray = Object.entries(convertToSet);
const cleanedArrayOfObjects = revertAsCleanedArray.map(([id, value]) => ({id, value}));
about 4 years ago · Juan Pablo Isaza Report

0

Get all unique values in a JavaScript array

let arr = [{id:34,value:45}, {id:23,value:35}, {id:34,value:28}]
var unique = arr.reverse().filter((v, i, a) => a.map(e => e.id).indexOf(v.id) === i);
console.log(unique);

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!