Javascript - Lodash / Underscore 比较两个对象并根据它们的键更改值
Javascript - Lodash / Underscore compare two objects and change values based on their keys
有个小问题
我需要比较两个对象,并基于这些对象中的相同键将值从第二个对象传递到原始对象。
原始对象
{
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"
}
检查合规性不需要密钥
输出会是这样的
{
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 );
有个小问题
我需要比较两个对象,并基于这些对象中的相同键将值从第二个对象传递到原始对象。
原始对象
{
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"
}
检查合规性不需要密钥
输出会是这样的
{
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 );