gpt4 book ai didi

javascript - 根据值将 javascript 对象数组分组到它们自己的对象子数组中

转载 作者:行者123 更新时间:2023-11-30 07:54:15 25 4
gpt4 key购买 nike

我有一个 Javascript 对象数组,如下所示。

        [{

"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{

"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{

"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},
{

"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}]

理想情况下,我想要的是将具有相同值(电子邮件)的那些分组到它们自己的对象子数组中,即,如果您查看上面的数组,您会看到我有 2 个条目用于同一个人 Alex McPherson。如果可能的话,我想做的是下面的移动并合并到一个子数组中,对于多次存在的任何其他值也是如此。

    [[{

"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "40",
"rate": "20",
"amount": "200",
"vat": "60",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
},{

"email": "alex@test.com",
"fn": "Alex",
"sn": "McPherson",
"phone": "01233xxxxx",
"hours": "90",
"rate": "30",
"amount": "900",
"vat": "120",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}],
[{

"email": "mike@test.com",
"fn": "Mike",
"sn": "Mann",
"phone": "01233xxxxx",
"hours": "50",
"rate": "70",
"amount": "500",
"vat": "90",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}],
[{

"email": "fred@test.com",
"fn": "Fred",
"sn": "Frogg",
"phone": "01233xxxxx",
"hours": "80",
"rate": "90",
"amount": "800",
"vat": "100",
"agency": "test",
"start": "08/06/2017",
"end": "10/06/2017"
}]]

我似乎无法理解如何使用数组。

最佳答案

您可以对同一电子邮件地址及其项目的哈希表使用闭包。

var data = [{ email: "alex@test.com", fn: "Alex", sn: "McPherson", phone: "01233xxxxx", hours: "40", rate: "20", amount: "200", vat: "60", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "mike@test.com", fn: "Mike", sn: "Mann", phone: "01233xxxxx", hours: "50", rate: "70", amount: "500", vat: "90", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "fred@test.com", fn: "Fred", sn: "Frogg", phone: "01233xxxxx", hours: "80", rate: "90", amount: "800", vat: "100", agency: "test", start: "08/06/2017", end: "10/06/2017" }, { email: "alex@test.com", fn: "Alex", sn: "McPherson", phone: "01233xxxxx", hours: "90", rate: "30", amount: "900", vat: "120", agency: "test", start: "08/06/2017", end: "10/06/2017" }],
result = data.reduce(function (hash) {
return function (r, o) {
if (!hash[o.email]) {
hash[o.email] = [];
r.push(hash[o.email]);
}
hash[o.email].push(o)
return r;
};
}(Object.create(null)), []);

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

关于javascript - 根据值将 javascript 对象数组分组到它们自己的对象子数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433833/

25 4 0
文章推荐: javascript - 如何从另一个 Controller 调用一个 Controller 的功能
文章推荐: java - 将变量合并到 标记中以在 CMS (Joomla) 中使用