Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading