3 回答
TA贡献1798条经验 获得超3个赞
这是一种方法,我不确定这是否是您需要的对象格式。
let docArray = [
{ documentID: 304717110, doublettenID: 111, done: true },
{ documentID: 304717110, doublettenID: 222, done: true },
{ documentID: 304717110, doublettenID: 333, done: true },
{ documentID: 304717110, doublettenID: 444, done: true },
{ documentID: 304717110, doublettenID: 555, done: true },
{ documentID: 304717110, doublettenID: 666, done: true },
{ documentID: 304862280, doublettenID: null, done: true },
{ documentID: 304862281, doublettenID: null, done: true },
{ documentID: 304933685, doublettenID: null, done: true },
{ documentID: 304933871, doublettenID: null, done: true },
{ documentID: 304933870, doublettenID: null, done: true },
{ documentID: 304862282, doublettenID: null, done: true }
]
var ids = []
var res = []
docArray.forEach(doc => {
if (!ids.includes(doc.documentID)) {
doc.doublettenID = [doc.doublettenID]
res.push(doc)
ids.push(doc.documentID)
} else {
res.find(d => doc.documentID = d.documentID).doublettenID.push(doc.doublettenID)
}
})
console.log(res)
TA贡献1815条经验 获得超6个赞
您可以遍历它并将documentID用作键,然后推送到一个单独的对象。
let docArray = [
{ documentID: 304717110, doublettenID: 111, done: true },
{ documentID: 304717110, doublettenID: 222, done: true },
{ documentID: 304717110, doublettenID: 333, done: true },
{ documentID: 304717110, doublettenID: 444, done: true },
{ documentID: 304717110, doublettenID: 555, done: true },
{ documentID: 304717110, doublettenID: 666, done: true },
{ documentID: 304862280, doublettenID: null, done: true },
{ documentID: 304862281, doublettenID: null, done: true },
{ documentID: 304933685, doublettenID: null, done: true },
{ documentID: 304933871, doublettenID: null, done: true },
{ documentID: 304933870, doublettenID: null, done: true },
{ documentID: 304862282, doublettenID: null, done: true }
];
let groups = {};
docArray.forEach(entry => {
if (!groups.hasOwnProperty(entry.documentID)) {
groups[entry.documentID] = [];
}
groups[entry.documentID].push(entry.doublettenID);
});
console.log(groups);
添加回答
举报