5 回答
TA贡献1836条经验 获得超4个赞
您缺少最后一个元素,因为您只迭代第一个数组并在第二个数组中搜索匹配项,但“Alvida”仅存在于第二个数组中。因此,您需要做的是在构建新数组时迭代两个数组。如果新数组中已存在属性,则对值求和,如果不存在则创建它。
说明性示例:
const results = {};
// arrayToInspect is key1, key2
Object.keys(demo).forEach(function(arrayToInspect) {
demo[arrayToInspect].forEach(function(element) {
if (results[element.name]) {
results[element.name].kill += element.kill;
results[element.name].knock += element.knock;
}
else {
results[element.name] = element;
}
});
});
然后,如果您需要它作为数组,您可以这样做:
const resultsArray = Object.values(results);
TA贡献1951条经验 获得超3个赞
Object.keys(demo).reduce
检查每个demo
键。demo[key].map
迭代每个 的数据"key"
。如果数据存在 => 对 和 求和
kill
,knock
否则 => 将其相加。
给你:D
let demo = {
"key1": [{
"id": 1,
"name": "Robbie",
"kill": 12,
"knock": 1
},
{
"id": 2,
"name": "Mohamand",
"kill": 12,
"knock": 4
},
{
"id": 3,
"name": "Jebisha",
"kill": 8,
"knock": 4
}
],
"key2": [{
"id": 1,
"name": "Robbie",
"kill": 12,
"knock": 4
},
{
"id": 2,
"name": "Mohamand",
"kill": 13,
"knock": 3
},
{
"id": 3,
"name": "Jebisha",
"kill": 5,
"knock": 1
},
{
"id": 4,
"name": "Alvida",
"kill": 8,
"knock": 5
}
]
};
let ans = [];
Object.keys(demo).reduce((acc, key) => {
demo[key].map(data => {
let found = acc.find(d => d.id === data.id);
if (found) {
found.kill += data.kill;
found.knock += data.knock;
} else {
acc.push(data);
}
});
return acc;
}, ans);
console.log(ans);
TA贡献1820条经验 获得超9个赞
这个怎么样?
使用地图收集数据然后计算总和
使用 [key, value] 迭代映射来构建数组
var demo = {
"key1": [{
"id": 1,
"name": "Robbie",
"kill": 12,
"knock": 1
},
{
"id": 2,
"name": "Mohamand",
"kill": 12,
"knock": 4
},
{
"id": 3,
"name": "Jebisha",
"kill": 8,
"knock": 4
}
],
"key2": [{
"id": 1,
"name": "Robbie",
"kill": 12,
"knock": 4
},
{
"id": 2,
"name": "Mohamand",
"kill": 13,
"knock": 3
},
{
"id": 3,
"name": "Jebisha",
"kill": 5,
"knock": 1
},
{
"id": 4,
"name": "Alvida",
"kill": 8,
"knock": 5
}
]
};
function merge(demo) {
let map = new Map()
let _calculate = function cal(arr) {
for (const item of arr) {
if (!map.has(item.id)) {
map.set(item.id, {id: item.id, name: item.name, kill: 0, knock: 0})
}
const obj = map.get(item.id);
obj.kill += item.kill
obj.knock += item.knock
map.set(item.id, obj)
}
}
Object.keys(demo).forEach(key => {
_calculate(demo[key])
})
let arr = []
for (const [key, value] of map) {
arr.push(value)
}
return arr;
}
console.log(merge(demo));
TA贡献1853条经验 获得超6个赞
const demo = {
"key1": [{"id": 1, "name": "Robbie", "kill": 12 , "knock": 1},
{"id": 2, "name": "Mohamand", "kill": 12 , "knock": 4},
{"id": 3, "name": "Jebisha", "kill": 8 , "knock": 4}],
"key2": [
{"id": 1, "name": "Robbie", "kill":12 , "knock": 4},
{"id": 2, "name": "Mohamand", "kill": 13 , "knock": 3},
{"id":3, "name": "Jebisha", "kill": 5 , "knock": 1},
{"id":4, "name": "Alvida", "kill": 8 , "knock": 5}]
};
const res = Object.values(demo).reduce((acc, value) => {
const currentElements = value;
currentElements.forEach((el) => {
if (!acc[el.id]) {
acc[el.id] = el
} else {
acc[el.id] = {
...acc[el.id],
"kill": acc[el.id]["kill"] + el["kill"],
"knock": acc[el.id]["knock"] + el["knock"],
}
}
});
return acc;
}, {});
console.log(res);
TA贡献1874条经验 获得超12个赞
你可以试试这个。这里有多少个键并不重要。当您维护一个对象来检查指定对象是否存在时,速度会更快。
var demo = {
"key1": [
{"id": 1, "name": "Robbie", "kill": 12 , "knock": 1},
{"id": 2, "name": "Mohamand", "kill": 12 , "knock": 4},
{"id": 3, "name": "Jebisha", "kill": 8 , "knock": 4}
],
"key2": [
{"id": 1, "name": "Robbie", "kill":12 , "knock": 4},
{"id": 2, "name": "Mohamand", "kill": 13 , "knock": 3},
{"id":3, "name": "Jebisha", "kill": 5 , "knock": 1},
{"id":4, "name": "Alvida", "kill": 8 , "knock": 5}
]
};
const res = {};
Object.values(demo).forEach(items => items.reduce((acc, curr) => {
if (res[curr.name] !== undefined) {
res[curr.name].kill += curr.kill;
res[curr.name].knock += curr.knock;
} else {
res[curr.name] = curr;
}
return res;
}, res));
const result = Object.values(res);
console.log(result);
.as-console-wrapper {min-height: 100%!important; top: 0}
添加回答
举报