我有用戶串列:
顯示代碼片段
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);
}
uj5u.com熱心網友回復:
您可以使用增加的名稱計數來映射新物件。第一個相同的名稱沒有數字。
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; }
uj5u.com熱心網友回復:
通常,我比@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結構來重命名用戶(或不重命名)。
uj5u.com熱心網友回復:
您可以使用物件來執行此操作并計算您想要的值。
const counts = {};
users.forEach((user) => {
counts[user.name] = (counts[user.name] || 0) 1;
});
console.log(counts)
uj5u.com熱心網友回復:
我相信您想要的是(為了清楚起見,我在索引之前添加了下劃線):
顯示代碼片段
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429101.html
標籤:javascript 数组 目的 减少 循环
上一篇:javascript:如何計算陣列內嵌套陣列的平均值
下一篇:在反應中顯示陣列資料。輸出不來
