Javascript lodash:对象中的深度合并
我想在此对象中进行合并:Javascript lodash:对象中的深度合并,javascript,object,lodash,Javascript,Object,Lodash,我想在此对象中进行合并: objs = { one: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }, two: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} } } objs = { one
objs = {
one: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
two: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}
objs = {
one: { original_description: "value", amount: 5, description: "value desc", identifier: "some text" },
two: { original_description: "value", amount: 5, description: "value desc", identifier: "some text" }
}
在本obj中:
objs = {
one: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
two: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}
objs = {
one: { original_description: "value", amount: 5, description: "value desc", identifier: "some text" },
two: { original_description: "value", amount: 5, description: "value desc", identifier: "some text" }
}
UPD:来自@Ryeball的解决方案正在运行,但我发现问题是,“子对象”与父对象由相同的键名组成。您可以使用,并有一个回调,通过使用并使用值本身返回每个省略了值的对象
var objs={
一:{description:“value”,amount:5,value:{identifier:“some text”},
二:{description:“value”,amount:5,value:{identifier:“some text”}
};
var result=js.mapValues(objs,i=>js(i).omit('value').merge(i.value).value());
document.write(“”+JSON.stringify(结果,0,4)+“”)代码>
还有一个问题(如果value=null,此函数将被破坏,需要添加检查item[mergeKey]!=null然后合并,错误示例:@ShootingStar请检查更新,我在回调中使用过。