如何将共享一个公共键的两个对象的两个值合并在一起并返回



编写一个名为mergeCounters的函数,该函数接受两个所有值都是数字的对象,并返回一个包含这两个对象总数的新对象。返回对象的顺序无关紧要,只要它包含正确的键和值!

function mergeCounters (object1, object2) {
var myObj = {};
for (key in obj1) {
for (key2 in obj2) {
var val = myObj[key + key2];
}
}
return myObj;
}
var obj1 = {a: 1, b:2, c:3};
var obj2 = {a: 3, b:6, c:7};
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = {a: 1, c:3, d:5};
var obj4 = {a: 3, b:6, c:7, z:20};
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

这里不需要嵌套循环,因为您可以使用键对两个对象的值进行索引/访问。由于object1可能具有比object2更多的属性,反之亦然,因此可以首先使用排列语法...object1object2中的所有对象属性合并为合并对象。一旦合并了对象,就可以使用for...in迭代对象的键,并从object1object2中获取键以添加到新的结果对象中。一个对象可能没有另一个键,因此如果不存在,可以使用|| 0将值默认为0

参见以下示例:

function mergeCounters (object1, object2) {
const merged = {...object1, ...object2};
const result = {};
for(const key in merged) {
result[key] = (object1[key] || 0) + (object2[key] || 0);
}
return result;
}
var obj1 = {a: 1, b:2, c:3};
var obj2 = {a: 3, b:6, c:7};
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = {a: 1, c:3, d:5};
var obj4 = {a: 3, b:6, c:7, z:20};
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

这很容易实现,过程如下。

  • object1的值复制到myObj
  • 迭代所有object2,如果密钥已经存在,则添加它,否则设置它

function mergeCounters (object1, object2) {
const myObj = { ...object1 };
for(var key in object2) {
myObj[key] = (myObj[key] || 0) + object2[key]
}
return myObj;
}
var obj1 = {a: 1, b:2, c:3};
var obj2 = {a: 3, b:6, c:7};
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = {a: 1, c:3, d:5};
var obj4 = {a: 3, b:6, c:7, z:20};
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

减少Nick Parsons解决方案的版本

function mergeCounters (object1, object2) {
return Object.keys({...object1, ...object2}) // collect all keys of both objects
.reduce((acc, key) => {
acc[key] = (object1[key] || 0) + (object2[key] || 0); // add the object values together if they exist
return acc;
},{})
}
var obj1 = {a: 1, b:2, c:3};
var obj2 = {a: 3, b:6, c:7};
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = {a: 1, c:3, d:5};
var obj4 = {a: 3, b:6, c:7, z:20};
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

对于较旧的浏览器,还有另一种方法可以给出预期的结果:

function mergeCounters(o1, o2, firstSum) {
let sum = firstSum || {};
for (let prop in o1) {
sum[prop] = sum[prop] === undefined ?
o1[prop] || 0 + o2[prop] || 0 :
sum[prop] += o1[prop] || o2[prop];
}
if (!firstSum) {
mergeCounters(o2, o1, sum);
}
return sum;
};
var obj1 = {a: 1, b:2, c:3};
var obj2 = {a: 3, b:6, c:7};
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = {a: 1, c:3, d:5};
var obj4 = {a: 3, b:6, c:7, z:20};
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

Object.entries()Array.prototype.concat()Array.prototype.reduce()可以帮助

  1. Object.entries():获取所有属性及其值
  2. Array.prototype.concat():组合两个对象的属性
  3. Array.prototype.reduce():将属性添加到一个新对象中,并组合具有相同名称的属性的值

function mergeCounters(objectA, objectB) {
const entriesA = Object.entries(objectA);
const entriesB = Object.entries(objectB);
return entriesA.concat(entriesB)
.reduce((result, current) => {
const key = current[0];
const value = current[1];
if (typeof result[key] !== "undefined") {
result[key] = result[key] + value;
} else {
result[key] = value;
}
return result
}, {});
}
var obj1 = { a: 1, b: 2, c: 3 };
var obj2 = { a: 3, b: 6, c: 7 };
console.log(mergeCounters(obj1, obj2)); // {a:4, b:8, c:10}
var obj3 = { a: 1, c: 3, d: 5 };
var obj4 = { a: 3, b: 6, c: 7, z: 20 };
console.log(mergeCounters(obj3, obj4)); // {a:4, b:6, c:10, d:5, z:20}

最新更新