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

标签 javascript arrays object

我有一个 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/

相关文章:

javascript - 如何在 JS/chrome API 中为任何 DOM 对象创建 onClick 监听器

javascript - 隐藏除所选系列之外的所有其他系列。还显示下拉列表中的项目列表

javascript - 计算一个对象数组中值在另一个对象数组中的出现次数

json - 我在 Eclipse 中的 .json 文件上收到 "Expected name at XX:YY"错误。怎么了?

scala - 如果单例如此糟糕,为什么 Scala 为它们提供语言支持?

javascript - React : Click text,动态附加表单并用它来重命名文本

javascript - 有没有一种简单的方法将复杂的 JS 对象转换为查询参数?

javascript - `react-apollo` `MockedProvider` 需要超时?

arrays - 在Mathematica中使用数组和表函数。什么时候最好

c - 如何将字符串 block 转换为字符串数组