5 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
您缺少最后一個(gè)元素,因?yàn)槟坏谝粋€(gè)數(shù)組并在第二個(gè)數(shù)組中搜索匹配項(xiàng),但“Alvida”僅存在于第二個(gè)數(shù)組中。因此,您需要做的是在構(gòu)建新數(shù)組時(shí)迭代兩個(gè)數(shù)組。如果新數(shù)組中已存在屬性,則對(duì)值求和,如果不存在則創(chuàng)建它。
說(shuō)明性示例:
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;
}
});
});
然后,如果您需要它作為數(shù)組,您可以這樣做:
const resultsArray = Object.values(results);

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
Object.keys(demo).reduce
檢查每個(gè)demo
鍵。demo[key].map
迭代每個(gè) 的數(shù)據(jù)"key"
。如果數(shù)據(jù)存在 => 對(duì) 和 求和
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貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
這個(gè)怎么樣?
使用地圖收集數(shù)據(jù)然后計(jì)算總和
使用 [key, value] 迭代映射來(lái)構(gòu)建數(shù)組
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貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
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貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
你可以試試這個(gè)。這里有多少個(gè)鍵并不重要。當(dāng)您維護(hù)一個(gè)對(duì)象來(lái)檢查指定對(duì)象是否存在時(shí),速度會(huì)更快。
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}
添加回答
舉報(bào)