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

87
Views
How can add properties to my objects based on the duplicates inside the array?

I have this array of objects

let arr = [
    {
        id: 1,
    },
    {
        id: 1,
    },
    {
        id: 2,
    },
    {
        id: 1,
    },
    {
        id:4,
    },
    {
        id: 3,
    },
    {
        id:4,
    }
]

i need to find and change every object in the array based on condition. So if there are duplicates in the array i need to set on my objects 100 except last duplicate where i should have 200. If i don't have any duplicates than i should have again 200

So the output shpuld be

let arr = [
    {
        id: 1,
        number: 100
    },
    {
        id: 1,
        number: 100
    },
    {
        id: 2,
        number: 200
    },
    {
        id: 1,
        number: 200
    },
    {
        id:4,
        number: 100
    },
    {
        id: 3,
        number: 200
    },
    {
        id:4,
        number: 200
    }
]

so id 1 has duplicates. That is why the fiurst occurences are set with number:100 and the last one i set with number:200.

Id 2 has number 200 because there are no duplicates and it is first occurance in the list.

what i tried

I got stuck at

for(let item of arr) {
    for(let item2 of arr) {
        if(item.id === item2.id) {
            item.number = 100;
        } else {
            item.number = 200;
        }
    }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can simply iterate through the array in reverse and track which ids you've seen, here using a Set.

const arr = [{ id: 1, }, { id: 1, }, { id: 2, }, { id: 1, }, { id: 4, }, { id: 3, }, { id: 4, }]

let i = arr.length;
const seen = new Set();

while (i--) {
  arr[i].number = seen.has(arr[i].id) ? 100 : 200;
  seen.add(arr[i].id)
}

console.log(arr)

about 4 years ago · Juan Pablo Isaza Report

0

You can use array.map() to iterate over your array. I think it can provide a nice and concise solution:

const result = arr.map((item, index) => {
  const duplicate = arr.filter((_, indx) => indx > index).some((i) => i.id === item.id);
  return { ...item, number: duplicate ? 100 : 200 }
});

console.log(result);
about 4 years ago · Juan Pablo Isaza Report

0

We can simply achieve it via Array.map() along with Array.indexOf() & Array.lastIndexOf() methods.

Working Demo :

// Input array
let arr = [{
  id: 1,
}, {
  id: 1,
}, {
  id: 2,
}, {
  id: 1,
}, {
  id:4,
}, {
  id: 3,
}, {
  id:4,
}];

// Getting ID's from each object and create a seperate array
let idArr = arr.map(function(item) { return item.id });

// Iterating through the id's array and assigning number property to an original array as per the requirement.
idArr.forEach((item, index) => {
  if (idArr.indexOf(item) === idArr.lastIndexOf(item)) {
    arr[index].number = 200;
  } else {
    arr[index].number = 100;
    arr[idArr.lastIndexOf(item)].number = 200;
  }  
});

// Result
console.log(arr);

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!