我想用Java脚本按ID合并两个对象,但是我不知道如何
var personData = {
person1 : {
uid: 112233,
city: "esfahan",
postalCode: 2345672345,
phoneNumber: "03111234234",
position: "ui designer"
}}
var additionalPersonData = {
person12: {
uid: 112233,
firstName: "reza",
lastName: "hosseini"
}}
因此输出应如下所示:
result = [
{
uid: 112233,
firstName: "reza",
lastName: "hosseini"
city: "isfahan",
postalCode: 2345672345,
phoneNumber: "03111234234",
position: "ui designer"
}]
答案 0 :(得分:0)
使用Object.assign():
var personData = {
person1: {
uid: 112233,
city: "esfahan",
postalCode: 2345672345,
phoneNumber: "03111234234",
position: "ui designer"
}
};
var additionalPersonData = {
person12: {
uid: 112233,
firstName: "reza",
lastName: "hosseini"
}
};
var result = [];
result.push(Object.assign(
{},
personData.person1,
additionalPersonData.person12
));
console.log(result);