如何通过添加出现次数使用户名唯一?

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

我有用户列表:

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

而且我希望用户名是唯一的。 如果名称例如“Leanne Graham”在那种情况下重复了很多次我想第一个添加到他的名字 1,第二个添加他的名字 2...
我试过这种方法:但它的顺序是 DESC(从出现次数较多到出现次数最少,我希望它按升序排列。

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

您可以使用对象并计算您想要的值来做到这一点。

const counts = {};
users.forEach((user) => {
  counts[user.name] = (counts[user.name] || 0) + 1;
});
console.log(counts)

我相信你想要的是(为了清楚起见,我在索引前添加了下划线):

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

您可以映射名称计数增加的新对象。同名不取号

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

一如既往,我比@Nina 慢得多:D 但这是我的两美分代码:

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",
  },
];

const cnts={};
let output = users.map(u=> {
  if(cnts[u.name])
    u.name+= cnts[u.name]++;
  else cnts[u.name]=1;
  return u;
});

console.log(output);

我还使用一个对象 (cnts) 来跟踪出现的次数。但是在 .map() 中,我采用了一种很好的老式 if/else 结构来重命名用户(或不重命名)。