假设
const listA = [{"key":"apple", "value":100}, {"key":"banana", "value":50}, {"key":"pearl", "value":10}, {"key":"cherry", "value":5}, {"key":"kiwi", "value":3}]
const listB = [{"key":"peach", "value":30}, {"key":"apple", "value":15}, {"key":"kiwi", "value":10}, {"key":"mango", "value":5}]
如您所见,在数组中,每个对象对都带有“键”和“值”,我希望在新的构建列表中堆叠同一个键的值,新的构建列表也需要放在按值desc排序,例如类似->
const listMerged = [{"key":"apple", "value":115}, {"key":"banana", "value":50} , {"key":"peach", "value":30}, {"key":"kiwi", "value":13}, {"key":"pearl", "value":10}, {"key":"cherry", "value":5}, {"key":"mango", "value":5}]
只需尝试这样的事情->
let newArr = listB.forEach((item) => {
let ifElemPresentInListA = listA.findIndex((elem) => {
return Object.keys(elem)[0] === Object.keys(item)[0]
})
if (ifElemPresentInListA === -1) {
listA.push(item)
} else {
for (let keys in listA[ifElemPresentInListA]) {
listA[ifElemPresentInListA][keys] += Object.values(item)[0]
}
}
})
但是结果有点混乱
如果有人可以提出更好的代码示例,那将是非常合适的。谢谢
答案 0 :(得分:2)
Here is a potential solution that concatenates the list, sorts by keys, and then iterates through the concatenated/sorted array to conditionally add or increment in a new list.
const listA = [{"key":"apple", "value":100}, {"key":"banana", "value":50}, {"key":"pearl", "value":10}, {"key":"cherry", "value":5}, {"key":"kiwi", "value":3}]
const listB = [{"key":"peach", "value":30}, {"key":"apple", "value":15}, {"key":"kiwi", "value":10}, {"key":"mango", "value":5}]
let lists = [];
listA.concat(listB).sort((a, b) => a.key > b.key).forEach(el => {
if (lists.length > 0 && lists[lists.length - 1].key === el.key) {
lists[lists.length - 1].value += el.value;
} else {
lists.push(el);
}
});
console.log(lists.sort((a, b) => b.value - a.value));
答案 1 :(得分:2)
You can zip two arrays into object to sum values for matching keys and then create array from object back:
const listA = [{"key":"apple", "value":100}, {"key":"banana", "value":50}, {"key":"pearl", "value":10}, {"key":"cherry", "value":5}, {"key":"kiwi", "value":3}];
const listB = [{"key":"peach", "value":30}, {"key":"apple", "value":15}, {"key":"kiwi", "value":10}, {"key":"mango", "value":5}];
let result = [...listA, ...listB].reduce((acc, item) => {
acc[item.key] = acc[item.key] ? acc[item.key] + item.value : item.value
return acc;
}, {});
result = Object.entries(result)
.map(([key, value]) => ({ key, value }))
.sort((a, b) => b.value - a.value);
console.log(result);
答案 2 :(得分:2)
You could take a Map and collect all key/value pairs and then sort the new array.
const
listA = [{ key: "apple", value: 100 }, { key: "banana", value: 50 }, { key: "pearl", value: 10 }, { key: "cherry", value: 5 }, { key: "kiwi", value: 3 }],
listB = [{ key: "peach", value: 30 }, { key: "apple", value: 15 }, { key: "kiwi", value: 10 }, { key: "mango", value: 5 }],
result = Array
.from(
[...listA, ...listB].reduce((m, { key, value }) => m.set(key, (m.get(key) || 0) + value), new Map),
([key, value]) => ({ key, value })
)
.sort(({ key: a }, { key: b }) => a.localeCompare(b));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }