我正在接收下一个数据
[
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
]
我想通过id合并它,并将rName推送到具有此结构的数组中
[
{ id: "1", name: "test1", rName: ["the1", "the2","the3"] },
{ id: "2", name: "test2", rName: ["the1", "the2"] },
{ id: "3", name: "test3", rName: ["the1"] }
]
我以为减少操作会成功,但没有成功,如果有人能指出正确的方向,那将不胜感激。
答案 0 :(得分:2)
这是重新格式化数据的非常简单的情况。代码在下面。
var data = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
];
var reduced = Object.values(data.reduce(function(accumulator, element) {
if (!accumulator[element.id]) {
accumulator[element.id] = { id: element.id, name: element.name, rName: [] };
}
accumulator[element.id].rName.push(element.rName);
return accumulator;
}, {}));
console.log(reduced);
累加器检查累加器中是否存在element.id
键。如果没有,它将创建它。然后,它将新的rName
推送到现有堆栈中。然后使用Object.values()
将转换返回到数组。
答案 1 :(得分:1)
在化简accumulator
中,检查是否存在与要迭代的当前项目相同的id
的项目。如果是,请将当前项目的rName
推入rName
数组。否则,将一个新项目推送到accumulator
var data = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
];
const newArray = data.reduce((acc, {id,name,rName}) => {
const existing = acc.find(a => a.id == id);
if (existing)
existing["rName"].push(rName);
else
acc.push({id,name,rName: [rName]})
return acc
}, []);
console.log(newArray)
这是一个单行代码高尔夫球的答案。 (从@Sébastien的答案中得到了想法):
var data = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test2", rName: "the2" },
{ id: "1", name: "test2", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test1", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
]
const anotherArray = Object.values(data.reduce((acc, {id,name,rName}) =>
((acc[id] = acc[id] || {id,name,rName:[]})["rName"].push(rName), acc), {}));
console.log(anotherArray)
答案 2 :(得分:0)
纯ES6
let result = obj.reduce((acc, item) => {
let found = acc.find(i => i.id === item.id);
found ? (found.rName = [...found.rName, item.rName]) : (acc = [...acc, { ...item, rName: [item.rName] }]);
return acc;
}, []);
答案 3 :(得分:0)
如果每个id
都可以确定一个唯一的name
,那么它将起作用:
let data = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
];
let result = [];
for (let item of data)
{
const index = result.findIndex(i => i.id === item.id);
if (index < 0) {
result.push({ id: item.id, name: item.name, rName: [item.rName] });
}
else {
result[index].rName.push(item.rName);
}
}
请注意,当以下数据应被接受时,这将不起作用:
[
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test2", rName: "the2" },
{ id: "1", name: "test2", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test1", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
]
答案 4 :(得分:0)
如果您坚持使用'reduce'方法,您可以这样做:
var arr = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
]
var arr1 = arr.reduce(function (a1, a2) {
if (a1 instanceof Array) {
let lastItem = a1[a1.length - 1]
if (lastItem.id == a2.id) {
lastItem.rName.push(a2.rName)
} else {
a1.push({ ...a2, rName: [a2.rName] })
}
return a1
} else {
let result = []
if (a1.id == a2.id) {
result.push({ ...a1, rName: [a1.rName, a2.rName] })
} else {
result.push({ ...a1, rName: [a1, rName] })
result.push({ ...a2, rName: [a2, rName] })
}
return result
}
})
console.log(JSON.stringify(arr1))
但是我认为您应该这样做,因为代码更清晰:
var arr = [
{ id: "1", name: "test1", rName: "the1" },
{ id: "1", name: "test1", rName: "the2" },
{ id: "1", name: "test1", rName: "the3" },
{ id: "2", name: "test2", rName: "the1" },
{ id: "2", name: "test2", rName: "the2" },
{ id: "3", name: "test3", rName: "the1" }
]
var map = new Map()
arr.forEach(function(item){
if(map.has(item.id)){
map.get(item.id).rName.push(item.rName)
}else{
map.set(item.id, {...item, rName: [item.rName]})
}
})
var arr1 = Array.from(map.values())
console.log(JSON.stringify(arr1))