[英]how to merge multiple array with id
大家好,我在分組數組時遇到了一些問題我有一個像這樣的 object 數組
var result= [
{
"user_id": 3,
"type_id": 1,
"total": 24,
"correct": 17,
"wrong": 7,
"not_attempt": 0,
"standard": "9",
"gender": "Male",
"name": "Mohammed"
},
{
"user_id": 4,
"type_id": 1,
"total": 24,
"correct": 4,
"wrong": 20,
"not_attempt": 0,
"standard": "7",
"gender": "Male",
"name": "Alex"
},
{
"user_id": 3,
"type_id": 3,
"total": 20,
"correct": 9,
"wrong": 11,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 3,
"total": 20,
"correct": 4,
"wrong": 16,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 4,
"total": 20,
"correct": 2,
"wrong": 18,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 4,
"total": 20,
"correct": 5,
"wrong": 15,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 5,
"total": 72,
"correct": 39,
"wrong": 33,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 5,
"total": 72,
"correct": 37,
"wrong": 35,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 6,
"total": 25,
"correct": 0,
"wrong": 25,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 6,
"total": 25,
"correct": 0,
"wrong": 25,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 7,
"total": 72,
"correct": 39,
"wrong": 33,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 7,
"total": 72,
"correct": 40,
"wrong": 32,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 8,
"total": 12,
"correct": 6,
"wrong": 6,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 8,
"total": 12,
"correct": 2,
"wrong": 10,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
}
]
我想按這個 object 按 user_id 和 type_id 和他們的分數分組我想要一個如下所示的結果
[
{
"user_id": 3,
"gender": "Male",
"name": "Mohammed",
"standard": "9",
"va":17,
"na":9,
"ca":2,
"sa":39,
"ma":0,
"cl":39,
"ra":6
},
{
"user_id": 4,
"gender": "Male",
"name": "Alex",
"standard": "7",
"va":4,
"na":4,
"ca":5,
"sa":37,
"ma":0,
"cl":40,
"ra":2
}
]
如果 user_id = 3 & type_id = 1 那么我希望像 va 一樣添加新密鑰,並且該 type_if 的正確分數將添加到該新密鑰相同的方法將適用於他們的 user_id 的所有 type_id,我嘗試了下面的代碼,但我不是得到我想要的結果
var newresult = result.reduce(function(results, org) {
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {})
我不知道如何計算分數 (va, na, ca, sa, ...)
但是你可以通過這種方式做到這一點
function initializeUserObject(row) {
const { user_id, gender, name, standard } = row;
return { user_id, gender, name, standard };
}
function calculateScore(row) {
const userScoreObject = {};
// TODO : calculate score
return userScoreObject;
}
const userMap = {}
result.forEach(row => {
if (!(row.user_id in userMap)) {
userMap[row.user_id] = initiailzeUserObject(row);
}
Object.assign(userMap[row.user_id], calculateScore(row));
})
const newResult = Object.values(userMap);
或減少
const newResult = Object.values(result.reduce((userMap, cur) => {
userMap[cur.user_id] = Object.assign(userMap[cur.user_id] || {}, cur)
}, {}));
// you should calculate score after merge
一般來說,您可以使用與以下相同的代碼
var newresult = result.reduce(function(results, org) {
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {})
對於僅包含 type_id 1 的列表,請嘗試以下操作
var type_id = 1;
var newresult = result.reduce(function(results, org) {
if(org.type_id == type_id && org.type_id != 'undefined' )
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {});
請注意,這將獲得 object 鍵,它們出現在結果變量 object 中,然后您可以循環並計算新結果的分數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.