const toBeUpdated = {
"stores": {
"city1": [{
"address": "13, Landiling",
"firstName": "Robot",
"lastName": "Tom",
"MiddleName": "Dian"
}]
}
};
const updateInfo = {
"stores": {
"city1": [{
"address": "13, Landiling",
"firstName": "Robot",
"lastName": "Tom",
"phone": "12345678",
"email": "test@",
"manager": "tim"
}]
}
};
需要使用缺少的键(新)和来自updateInfo对象的值来更新toBeUpdated
对象。
答案 0 :(得分:1)
您可以使用ES6传播运算符合并两个对象。试试这个
const toBeUpdated = {
"stores": {
"city1": [{
"address": "13, Landiling",
"firstName": "Robot",
"lastName": "Tom",
"MiddleName": "Dian"
}]
}
};
const updateInfo = {
"stores": {
"city1": [{
"address": "13, Landiling",
"firstName": "Robot",
"lastName": "Tom",
"phone": "12345678",
"email": "test@",
"manager": "tim"
}]
}
};
const updatedCity = toBeUpdated.stores.city1.map((city, i) => ({...city, ...updateInfo.stores.city1[i]}));
toBeUpdated.stores.city1 = updatedCity;
console.log(updatedCity);