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

240
Views
How to make name of users uniques by adding number of occurrence?

I've list of users:

const users = [
  {
    id: 1,
    name: "Leanne Graham",
    phone: "1-770-736-8031 x56442",
  },
  {
    id: 2,
    name: "Ervin Howell",
    phone: "010-692-6593 x09125",
  },
  {
    id: 3,
    name: "Leanne Graham",
    phone: "1-463-123-4447",
  },
  {
    id: 4,
    name: "Leanne Graham",
    phone: "493-170-9623 x156",
  },
  {
    id: 5,
    name: "Chelsey Dietrich",
    phone: "(254)954-1289",
  },
  {
    id: 6,
    name: "Mrs. Dennis Schulist",
    phone: "1-477-935-8478 x6430",
  },
  {
    id: 7,
    name: "Kurtis Weissnat",
    phone: "210.067.6132",
  },
  {
    id: 8,
    name: "Nicholas Runolfsdottir V",
    phone: "586.493.6943 x140",
  },
  {
    id: 9,
    name: "Glenna Reichert",
    phone: "(775)976-6794 x41206",
  },
  {
    id: 10,
    name: "Leanne Graham",
    phone: "024-648-3804",
  },
];
for (const user of users) {
  let countUserOccured = users.reduce(
    (acc, curr) => (acc += curr.name == user.name),
    0
  );
  if (countUserOccured - 1 != 0) {
    user.name += countUserOccured;
  }
  console.log(user);
}

And I want users name be unique. if the name e.g. "Leanne Graham" in that case repeated many times I want first one add to his name 1, second one his name 2...
I've tried this approach: but its order DESC(from greater number of occurrence to the lowest I want it in order ASCending.

for (const user of users) {
  let countUserOccured = users.reduce(
    (acc, curr) => (acc += curr.name == user.name),
    0
  );
  if (countUserOccured - 1 != 0) {
    user.name += countUserOccured;
  }
  console.log(user);
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could map new objects with incremented name count. The first same name does not get a number.

const
    users = [{ id: 1, name: "Leanne Graham", phone: "1-770-736-8031 x56442" }, { id: 2, name: "Ervin Howell", phone: "010-692-6593 x09125" }, { id: 3, name: "Leanne Graham", phone: "1-463-123-4447" }, { id: 4, name: "Leanne Graham", phone: "493-170-9623 x156" }, { id: 5, name: "Chelsey Dietrich", phone: "(254)954-1289" }, { id: 6, name: "Mrs. Dennis Schulist", phone: "1-477-935-8478 x6430" }, { id: 7, name: "Kurtis Weissnat", phone: "210.067.6132" }, { id: 8, name: "Nicholas Runolfsdottir V", phone: "586.493.6943 x140" }, { id: 9, name: "Glenna Reichert", phone: "(775)976-6794 x41206" }, { id: 10, name: "Leanne Graham", phone: "024-648-3804" }],
    counts = {},
    result = users.map(o => ({ ...o, name: o.name + (counts[o.name] ? ++counts[o.name] : (counts[o.name] = 1, '')) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

you can do that with objects and counting the value that you want.

const counts = {};
users.forEach((user) => {
  counts[user.name] = (counts[user.name] || 0) + 1;
});
console.log(counts)
about 4 years ago · Juan Pablo Isaza Report

0

I believe what you want is (I added underscore before index for clarity):

const userNamesMap = {};

const users = [
  {
    id: 1,
    name: "Leanne Graham",
    phone: "1-770-736-8031 x56442",
  },
  {
    id: 2,
    name: "Ervin Howell",
    phone: "010-692-6593 x09125",
  },
  {
    id: 3,
    name: "Leanne Graham",
    phone: "1-463-123-4447",
  },
  {
    id: 4,
    name: "Leanne Graham",
    phone: "493-170-9623 x156",
  },
  {
    id: 5,
    name: "Chelsey Dietrich",
    phone: "(254)954-1289",
  },
  {
    id: 6,
    name: "Mrs. Dennis Schulist",
    phone: "1-477-935-8478 x6430",
  },
  {
    id: 7,
    name: "Kurtis Weissnat",
    phone: "210.067.6132",
  },
  {
    id: 8,
    name: "Nicholas Runolfsdottir V",
    phone: "586.493.6943 x140",
  },
  {
    id: 9,
    name: "Glenna Reichert",
    phone: "(775)976-6794 x41206",
  },
  {
    id: 10,
    name: "Leanne Graham",
    phone: "024-648-3804",
  },
].map(user => {
  if (userNamesMap[user.name]) {
    user.name += `_${userNamesMap[user.name]++}`
  } else {
    userNamesMap[user.name] = 1;
  }
  return user;
});

console.log(users);

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!