合并两个具有相似键的 Javascript 对象
Merging two Javascript objects with similar keys
我需要合并两个碰巧共享相似键的对象 (obj1, obj2
)。
obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
}
obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
}
预期结果:
obj3 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"},
3:{"Example2": "Example2"},
4:{"Example2": "Example2"},
5:{"Example2": "Example2"},
}
合并两个对象时的常用方法:
const obj3 = Object.assign({}, obj1, obj2);
问题:它们确实共享许多密钥,因此,在obj3
中,只能找到obj2
的内容。
我的做法:
let obj3= Object.assign({}, obj1);
for(let i=0; i<obj2.length; i++) {
obj3[obj3.length + i] = obj2[i];
}
问题:是否有另一种更优雅、预定义的方法来合并具有相似键的两个对象?
因为您有键值映射而不是数组,所以您不能只组合对象。您唯一的方法是遍历所有键并将它们添加到最终结果。
例如沿线的东西:
const obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
};
const obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
};
function merge(...args) {
return Object.fromEntries( // Create entries
args // From all arguments
.flatMap(o => Object.values(o)) // Get values (discard keys)
.map((element, index) => [ index, element ]) // Remap them
);
}
console.log(merge(obj1, obj2));
// Will output
// {
// 0: { Example1: "Example1" },
// 1: { Example1: "Example1" },
// 2: { Example1: "Example1" },
// 3: { Example2: "Example2" },
// 4: { Example2: "Example2" },
// 5: { Example2: "Example2" }
// }
您代码中的对象是键值对,而不是简单的列表(数组)。
目前看来只有两种可能的情况:
- 你的对象有一个有意义的、唯一的关联键(例如,Whosebug 上的这个问题有键 69316153——看看 URL 栏)。在这种情况下,您真的不能合并这两者,因为它们有冲突的键。想想这个网站上有没有和这个ID相同的问题!
- 键没有意义,您很高兴为同一对象重新分配不同的键。在这种情况下,要使用的正确数据结构是数组 (
obj1 = [{"Example": "..."}, {"Example": "..."}]
).
如果后者是你的情况,此代码将起作用:
const obj3 = Object.values(obj1).concat(Object.values(obj2))
Object.values(obj)
returns 一个值数组,丢弃所有键。
假设我们有:
const obj1 = {
1: { Name: "Apple" },
2: { Name: "Watermelon" },
};
const obj2 = {
2: { Name: "Pear" },
5: { Name: "Tomato" }
};
Object.values(obj1)
将 return [{ Name: "Apple" }, { Name: "Watermelon" }]
,而 Object.values(obj2)
将 return [{ Name: "Pear" }, { Name: "Tomato" }]
.
使用 const obj3 = Object.values(obj1).concat(Object.values(obj2))
你将得到:
obj3 = [
{ Name: "Apple" },
{ Name: "Watermelon" },
{ Name: "Pear" },
{ Name: "Tomato" }
];
虽然我还是觉得obj1
和obj2
应该是数组...
const obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
};
const obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
}
const result = Object.fromEntries(
Object.values(obj1) // get the values of the first object
.concat(Object.values(obj2)) // get the values of the second object and add them to the values of the first
.map((value, index) => [ index, value ]) // re-index them
// or if you need actual copies of the "inner" objects
/*
.map((value, index) => [
index,
Object.assign({}, value)
])
*/
);
console.log(result);
这是“更优雅”吗。也许...
我需要合并两个碰巧共享相似键的对象 (obj1, obj2
)。
obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
}
obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
}
预期结果:
obj3 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"},
3:{"Example2": "Example2"},
4:{"Example2": "Example2"},
5:{"Example2": "Example2"},
}
合并两个对象时的常用方法:
const obj3 = Object.assign({}, obj1, obj2);
问题:它们确实共享许多密钥,因此,在obj3
中,只能找到obj2
的内容。
我的做法:
let obj3= Object.assign({}, obj1);
for(let i=0; i<obj2.length; i++) {
obj3[obj3.length + i] = obj2[i];
}
问题:是否有另一种更优雅、预定义的方法来合并具有相似键的两个对象?
因为您有键值映射而不是数组,所以您不能只组合对象。您唯一的方法是遍历所有键并将它们添加到最终结果。
例如沿线的东西:
const obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
};
const obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
};
function merge(...args) {
return Object.fromEntries( // Create entries
args // From all arguments
.flatMap(o => Object.values(o)) // Get values (discard keys)
.map((element, index) => [ index, element ]) // Remap them
);
}
console.log(merge(obj1, obj2));
// Will output
// {
// 0: { Example1: "Example1" },
// 1: { Example1: "Example1" },
// 2: { Example1: "Example1" },
// 3: { Example2: "Example2" },
// 4: { Example2: "Example2" },
// 5: { Example2: "Example2" }
// }
您代码中的对象是键值对,而不是简单的列表(数组)。
目前看来只有两种可能的情况:
- 你的对象有一个有意义的、唯一的关联键(例如,Whosebug 上的这个问题有键 69316153——看看 URL 栏)。在这种情况下,您真的不能合并这两者,因为它们有冲突的键。想想这个网站上有没有和这个ID相同的问题!
- 键没有意义,您很高兴为同一对象重新分配不同的键。在这种情况下,要使用的正确数据结构是数组 (
obj1 = [{"Example": "..."}, {"Example": "..."}]
).
如果后者是你的情况,此代码将起作用:
const obj3 = Object.values(obj1).concat(Object.values(obj2))
Object.values(obj)
returns 一个值数组,丢弃所有键。
假设我们有:
const obj1 = {
1: { Name: "Apple" },
2: { Name: "Watermelon" },
};
const obj2 = {
2: { Name: "Pear" },
5: { Name: "Tomato" }
};
Object.values(obj1)
将 return [{ Name: "Apple" }, { Name: "Watermelon" }]
,而 Object.values(obj2)
将 return [{ Name: "Pear" }, { Name: "Tomato" }]
.
使用 const obj3 = Object.values(obj1).concat(Object.values(obj2))
你将得到:
obj3 = [
{ Name: "Apple" },
{ Name: "Watermelon" },
{ Name: "Pear" },
{ Name: "Tomato" }
];
虽然我还是觉得obj1
和obj2
应该是数组...
const obj1 = {
0:{"Example1": "Example1"},
1:{"Example1": "Example1"},
2:{"Example1": "Example1"}
};
const obj2 = {
0:{"Example2": "Example2"},
1:{"Example2": "Example2"},
2:{"Example2": "Example2"}
}
const result = Object.fromEntries(
Object.values(obj1) // get the values of the first object
.concat(Object.values(obj2)) // get the values of the second object and add them to the values of the first
.map((value, index) => [ index, value ]) // re-index them
// or if you need actual copies of the "inner" objects
/*
.map((value, index) => [
index,
Object.assign({}, value)
])
*/
);
console.log(result);
这是“更优雅”吗。也许...