有一个小问题
我需要比较两个对象,并根据这些对象中的相同键将值从第二个对象传递到原始对象。
原始对象
{
content: "Old Value",
height: 200,
id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
type: "location",
units: "some unit value",
width: 200,
}
第二个对象
{
units: "some unit value",
content: "New Value"
}
检查合规性不需要 key
输出会是这样的
{
content: "New Value", // changed value
height: 200,
id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
type: "location",
units: "some unit value", // changed value
width: 200,
}
Lodash 或 Underscore 有没有优雅的解决方案来解决这个问题?
最佳答案
这就能解决问题(使用 lodash)
var source = {
content: "Old Value",
height: 200,
id: "15a2d286-d123-1cf0-4e40-b043b055a49f",
type: "location",
units: "some unit value",
width: 200,
},
source2 = {
units: "some unit value",
content: "New Value"
}
_.assignIn(source , source2 );
关于Javascript - Lodash/Underscore 比较两个对象并根据它们的键更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990865/