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

129
Views
how to retrieve the last unique object in an array

Hey guys is there a way i can retrieve the last unique object in an array in the code below the id at the first and second index are the same is there a way i can retrieve the last occurrence of the id with the corresponding object

0: {id: 'tra.528555295', name: 'heart'}
1: {id: 'tra.528555295', name: 'heart-outline'}
2: {id: 'tra.528555301', name: 'heart'}
3: {id: 'tra.528555301', name: 'heart-outline'}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to iterate through the entire array and keep track of the "last" object with that unique ID found.

Here's one way you can do it using Array.prototype.reduce to iterate through the array and keep track of the "last" ID found, then pulling values with unique IDs using Object.values:

const arr = [
  { id: "tra.528555295", name: "heart" },
  { id: "tra.528555295", name: "heart-outline" },
  { id: "tra.528555301", name: "heart" },
  { id: "tra.528555301", name: "heart-outline" }
];

const result = Object.values(
  arr.reduce((accumulator, item) => {
    const { id, ...rest } = item;
    return {
      ...accumulator,
      [id]: { id, ...rest }
    };
  }, {})
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

If you can provide the id I suggest:

const findLastElementOfId = (arr, id) => {
    return arr.filter(object => object.id === id).at(-1)
}

filter the array for the objects with the id you are looking for and return the last one.

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!