gpt4 book ai didi

javascript - 按多个属性对数组对象进行分组

转载 作者:行者123 更新时间:2023-11-30 14:55:37 25 4
gpt4 key购买 nike

我正在尝试将以下数组与基于 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]};
});

我知道我遗漏了一些东西,但无法弄清楚。有什么建议吗?

最佳答案

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

使用相同 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; }

关于javascript - 按多个属性对数组对象进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47316604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com