you need to manipulate your array to get the result.(您需要操纵数组以获得结果。)
You can try like below,(您可以尝试以下操作,)
var data = [ { compID: 1, problem: 1, usersID: 1, completed: 1 },
{ compID: 1, problem: 1, usersID: 5, completed: 1 },
{ compID: 1, problem: 2, usersID: 1, completed: 0 },
{ compID: 1, problem: 2, usersID: 5, completed: 1 },
{ compID: 1, problem: 3, usersID: 1, completed: 1 },
{ compID: 1, problem: 3, usersID: 5, completed: 0 },
{ compID: 1, problem: 4, usersID: 1, completed: 0 },
{ compID: 1, problem: 4, usersID: 5, completed: 1 },
{ compID: 1, problem: 5, usersID: 1, completed: 1 },
{ compID: 1, problem: 5, usersID: 5, completed: 0 } ]
var res ={};
data.map(d => {
if(res[d.usersID]) {
res[d.usersID].problem.push(d.problem);
res[d.usersID].completed.push(d.completed);
} else {
res[d.usersID] = {compID: d.compID, problem: [d.problem], usersID: d.usersID, completed: [d.completed] }
}
})
console.log(Object.values(res))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…