我有兩組資料需要以某種方式組合和分組。
第一組資料:
let overallCert =
[
{
"id": "1",
"entity_id": "3",
"status": "Certified",
},
{
"id": "379",
"entity_id": "417",
"status": "Certified",
}
];
第二組資料:
let userCerts =
[
{
"id": "640",
"entity_id": "417",
"method": "Field Study",
"strand": "",
"date_completed": "2016-07-15T08:00:00.000Z"
},
{
"id": "814",
"entity_id": "417",
"method": "Field Study",
"date_completed": "2019-07-15T08:00:00.000Z"
},
{
"id": "844",
"entity_id": "3",
"method": "Online",
"date_completed": "2022-03-28T08:00:00.000Z"
},
{
"id": "845",
"entity_id": "3",
"method": "Field Study",
"date_completed": "2022-03-28T08:00:00.000Z"
}
];
我想通過entity_id在下面創建此輸出來合并和分組這些物件陣列...
期望的輸出:
let desiredOutput =
[
[
[
{
"id": "640",
"entity_id": "417",
"method": "Field Study",
"date_completed": "2016-07-15T08:00:00.000Z"
},
{
"id": "814",
"entity_id": "417",
"method": "Field Study",
"date_completed": "2019-07-15T08:00:00.000Z"
},
],
[
{
"id": "379",
"entity_id": "417",
"status": "Certified",
}
]
],
[
[
{
"id": "844",
"entity_id": "3",
"method": "Online",
"date_completed": "2022-03-28T08:00:00.000Z"
},
{
"id": "845",
"entity_id": "3",
"method": "Field Study",
"date_completed": "2022-03-28T08:00:00.000Z"
}
],
[
{
"id": "379",
"entity_id": "417",
"status": "Certified",
}
]
]
];
到目前為止,我已經做到了:
let certsDataCombined = overallCert.map(item => ({ ...item,
0 : userCerts.filter(c => c.entity_id == item.entity_id)
}));
let userCertsGroupBy = groupBy(certsDataCombined, "entity_id");
function groupBy(arr, prop) {
const map = new Map(Array.from(arr, obj => [obj[prop], []]));
arr.forEach(obj => map.get(obj[prop]).push(obj));
return Array.from(map.values());
}
這段代碼幾乎可以解決問題,但我需要將overallCert資料封裝在它自己的陣列中,而且嵌套有點偏離。
是當前輸出:
uj5u.com熱心網友回復:
似乎您可以通過將每個映射overallCert到一個新陣列然后userCerts使用過濾器拉出匹配來構建輸出
const overallCert = [{"id":"1","entity_id":"3","status":"Certified"},{"id":"379","entity_id":"417","status":"Certified"}];
const userCerts = [{"id":"640","entity_id":"417","method":"Field Study","strand":"","date_completed":"2016-07-15T08:00:00.000Z"},{"id":"814","entity_id":"417","method":"Field Study","date_completed":"2019-07-15T08:00:00.000Z"},{"id":"844","entity_id":"3","method":"Online","date_completed":"2022-03-28T08:00:00.000Z"},{"id":"845","entity_id":"3","method":"Field Study","date_completed":"2022-03-28T08:00:00.000Z"}];
const result = overallCert.map(cert => [
userCerts.filter(({ entity_id }) => cert.entity_id === entity_id),
[ cert ]
]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; }
請注意,訂單是基于overallCert而不是userCerts像您的問題。
以上是O(n^2)時間復雜度,這并不好。您可以通過首先將兩個陣列按entity_idfor O(n)分組來改進這一點。這也可以讓你userCerts在你的例子中按like排序。
const overallCert = [{"id":"1","entity_id":"3","status":"Certified"},{"id":"379","entity_id":"417","status":"Certified"}];
const userCerts = [{"id":"640","entity_id":"417","method":"Field Study","strand":"","date_completed":"2016-07-15T08:00:00.000Z"},{"id":"814","entity_id":"417","method":"Field Study","date_completed":"2019-07-15T08:00:00.000Z"},{"id":"844","entity_id":"3","method":"Online","date_completed":"2022-03-28T08:00:00.000Z"},{"id":"845","entity_id":"3","method":"Field Study","date_completed":"2022-03-28T08:00:00.000Z"}];
// Helper function since Map is missing this
Map.prototype.getOrDefault = function(key, defaultValue) {
return (this.has(key) ? this : this.set(key, defaultValue)).get(key);
};
// Groups certs by entity_id
const reducer = (map, cert) => (
map.getOrDefault(cert.entity_id, []).push(cert),
map
);
const overallCertsByEntityId = overallCert.reduce(reducer, new Map());
const userCertByEntityId = userCerts.reduce(reducer, new Map());
const result = Array.from(userCertByEntityId, ([key, certs]) => [
certs,
overallCertsByEntityId.get(key)
]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/453256.html
標籤:javascript 数组 javascript 对象
下一篇:根據條件應用函式的最有效方法
