按多个属性分组数组对象

时间:2017-11-15 20:24:23

标签: javascript lodash

我尝试使用基于siteId的对象对以下数组进行分组:

var Details = [
   {
       "addressId": "399906",
       "extAddressId": null,
       "addressType": "ORDER_FULFILLMENT",
       "siteId": 101,
       "bankAccount": [
              {"bankAccountId": "409539","extBankAccountId":null,"primary": true},
              {"bankAccountId": "409537","extBankAccountId": null, "primary": false},
              {"bankAccountId": "399907", "extBankAccountId": null, "primary": false}
       ],
       "contactId": ["399908"],
       "extContactId": null,
       "emailForPurchaseOrders": "test@test.com",
       "emailForRemittance": "example@example.com",
       "emailLanguage": "English"
   },
   {
       "addressId": "399906",
       "extAddressId": null,
       "addressType": "LEGAL",
       "siteId": 101,
       "bankAccount": [
             {"bankAccountId": "399907", "extBankAccountId": null, "primary": false}
             {"bankAccountId": "409540","extBankAccountId":null,"primary": true},
       ],
       "contactId": [],
       "extContactId": null,
       "emailForPurchaseOrders": "example@example.com",
       "emailForRemittance": "test@test.com",
       "emailLanguage": "English"
   }
]

对于这样的事情:

{
   "addressId": ["399906"],
   "addressType": ["ORDER_FULFILLMENT", "LEGAL"],
   "siteId": 101,
   "bankAccount": [
        {
           "bankAccountId": "409539",
           "extBankAccountId": null,
           "primary": true
        },
        {
           "bankAccountId": "409537",
           "extBankAccountId": null,
           "primary": false
        },
        {
           "bankAccountId": "399907",
           "extBankAccountId": null,
           "primary": false
        },
        {
            "bankAccountId":"409540",
            "extBankAccountId":null,
            "primary": true
        },
    ],
    "contactId": ["399908"],
    "emailForPurchaseOrders": ["test@test.com", "example@example.com"],
    "emailForRemittance": ["example@example.com","test@test.com"],
    "emailLanguage": "English"
},

现在我试图将它分组,但无法获得上述结构以满足我的需求。到目前为止,这是我想要实现它的目的。任何帮助都非常感谢。

       var group_to_values = subscriptionDetail.reduce(function (obj, item) {
            obj[item.siteId] = obj[item.siteId] || [];
            obj[item.siteId].push(item);
            return obj;
        }, {});
        console.log(group_to_values);
        var groups = Object.keys(group_to_values).map(function (key) {
            return {siteId: key, details: group_to_values[key]};
        });

我知道我错过了一些东西,但无法弄明白。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您可以将一些辅助数组用于目标属性的所需类型。

使用相同siteId的哈希表进行扩展。



var data = [{ addressId: "399906", extAddressId: null, addressType: "ORDER_FULFILLMENT", siteId: 101, bankAccount: [{ bankAccountId: "409539", extBankAccountId: null, primary: true }, { bankAccountId: "409537", extBankAccountId: null, primary: false }, { bankAccountId: "399907", extBankAccountId: null, primary: false }], contactId: ["399908"], extContactId: null, emailForPurchaseOrders: "test@test.com", emailForRemittance: "example@example.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "LEGAL", siteId: 101, bankAccount: [{ bankAccountId: "399907", extBankAccountId: null, primary: false }, { bankAccountId: "409540", extBankAccountId: null, primary: true }], contactId: [], extContactId: null, emailForPurchaseOrders: "example@example.com", emailForRemittance: "test@test.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "ORDER_FULFILLMENT", siteId: 102, bankAccount: [{ bankAccountId: "409539", extBankAccountId: null, primary: true }, { bankAccountId: "409537", extBankAccountId: null, primary: false }, { bankAccountId: "399907", extBankAccountId: null, primary: false }], contactId: ["399908"], extContactId: null, emailForPurchaseOrders: "test@test.com", emailForRemittance: "example@example.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "LEGAL", siteId: 102, bankAccount: [{ bankAccountId: "399907", extBankAccountId: null, primary: false }, { bankAccountId: "409540", extBankAccountId: null, primary: true }], contactId: [], extContactId: null, emailForPurchaseOrders: "example@example.com", emailForRemittance: "test@test.com", emailLanguage: "English" }],
    hash = Object.create(null),
    result = [],
    singleKeys = ['siteId', 'emailLanguage'];

data.forEach(function (o) {
    if (!hash[o.siteId]) {
        hash[o.siteId] = {};
        result.push(hash[o.siteId]);
    }
    Object.keys(o).forEach(function (k) {
        if (o[k] === null) {
            return;
        }
        if (singleKeys.indexOf(k) !== -1) {
            hash[o.siteId][k] = o[k];
            return;
        }
        hash[o.siteId][k] = hash[o.siteId][k] || [];
        if (Array.isArray(o[k])) {
            Array.prototype.push.apply(hash[o.siteId][k], o[k]);
            return;
        }
        if (hash[o.siteId][k].indexOf(o[k]) === -1) {
            hash[o.siteId][k].push(o[k]);
        }
    });
});

console.log(result);

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




通过迭代数组以插入对象并检查是否已存在相同的bankAccountId来防止具有相同bankAccountId的重复对象的代码。如果没有将实际对象推送到数组。



var data = [{ addressId: "399906", extAddressId: null, addressType: "ORDER_FULFILLMENT", siteId: 101, bankAccount: [{ bankAccountId: "409539", extBankAccountId: null, primary: true }, { bankAccountId: "409537", extBankAccountId: null, primary: false }, { bankAccountId: "399907", extBankAccountId: null, primary: false }], contactId: ["399908"], extContactId: null, emailForPurchaseOrders: "test@test.com", emailForRemittance: "example@example.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "LEGAL", siteId: 101, bankAccount: [{ bankAccountId: "399907", extBankAccountId: null, primary: false }, { bankAccountId: "409540", extBankAccountId: null, primary: true }], contactId: [], extContactId: null, emailForPurchaseOrders: "example@example.com", emailForRemittance: "test@test.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "ORDER_FULFILLMENT", siteId: 102, bankAccount: [{ bankAccountId: "409539", extBankAccountId: null, primary: true }, { bankAccountId: "409537", extBankAccountId: null, primary: false }, { bankAccountId: "399907", extBankAccountId: null, primary: false }], contactId: ["399908"], extContactId: null, emailForPurchaseOrders: "test@test.com", emailForRemittance: "example@example.com", emailLanguage: "English" }, { addressId: "399906", extAddressId: null, addressType: "LEGAL", siteId: 102, bankAccount: [{ bankAccountId: "399907", extBankAccountId: null, primary: false }, { bankAccountId: "409540", extBankAccountId: null, primary: true }], contactId: [], extContactId: null, emailForPurchaseOrders: "example@example.com", emailForRemittance: "test@test.com", emailLanguage: "English" }],
    hash = Object.create(null),
    result = [],
    singleKeys = ['siteId', 'emailLanguage'];

data.forEach(function (o) {
    if (!hash[o.siteId]) {
        hash[o.siteId] = {};
        result.push(hash[o.siteId]);
    }
    Object.keys(o).forEach(function (k) {
        if (o[k] === null) {
            return;
        }
        if (singleKeys.indexOf(k) !== -1) {
            hash[o.siteId][k] = o[k];
            return;
        }
        hash[o.siteId][k] = hash[o.siteId][k] || [];
        if (k === 'bankAccount') {
            o[k].forEach(function (a) {
                var found = hash[o.siteId][k].some(function (b) {
                    return a.bankAccountId === b.bankAccountId;
                });
                if (!found) {
                    hash[o.siteId][k].push(a);
                }
            });
            return;
        }
        if (Array.isArray(o[k])) {
            Array.prototype.push.apply(hash[o.siteId][k], o[k]);
            return;
        }
        if (hash[o.siteId][k].indexOf(o[k]) === -1) {
            hash[o.siteId][k].push(o[k]);
        }
    });
});

console.log(result);

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




答案 1 :(得分:0)

`

 var Details = [
    {
        "addressId": "399906",
        "extAddressId": null,
        "addressType": "ORDER_FULFILLMENT",
        "siteId": 101,
        "bankAccount": [
                {"bankAccountId": "409539","extBankAccountId":null,"primary": true},
                {"bankAccountId": "409537","extBankAccountId": null, "primary": false},
                {"bankAccountId": "399907", "extBankAccountId": null, "primary": false}
        ],
        "contactId": ["399908"],
        "extContactId": null,
        "emailForPurchaseOrders": "test@test.com",
        "emailForRemittance": "example@example.com",
        "emailLanguage": "English"
    }
    ]

    let detailTwo = Details.map(item => {
    let keys = Object.keys(item);
    let copy = Object.assign({}, item);
    keys.forEach( val => {
        if(Array.isArray(copy[val]) && val !== 'bankAccount') {
            copy[val] = copy[val][0];  
        }
        if(copy[val] === null) {
            delete copy[val];  
        }
    });
return copy;
});

console.log(detailTwo);

`