[英]Lodash. Smart merge two objects by keys
I have the two objects. 我有两个对象。 A and B.
A和B。
A 一种
{
"beta" : {
"value": null,
"error" : null
},
"hamma" : {
"value": null,
"error" : null
},
"zerta" : {
"value": null,
"error" : null
},
"mozes" : 5
}
B 乙
{
"beta" : 5,
"hamma" : 2
}
How do I can loop through the A keys, compare it with the B object and update the values of the existing keys in the A object via Lodash? 如何遍历A键,将其与B对象进行比较并通过Lodash更新A对象中现有键的值? Maybe there is exists some nice way?
也许存在一些不错的方法? I tried to use "assing, assignWith" but looks like I haven't understood how it works.
我尝试使用“ assing,assignWith”,但似乎我不了解它的工作原理。
The result should looks like that: 结果应如下所示:
{
"beta" : {
"value": 5,
"error" : null
},
"hamma" : {
"value": 2,
"error" : null
},
"zerta" : {
"value": null,
"error" : null
},
"mozes" : 5
}
Thanks for any information. 感谢您提供任何信息。
I have resolved this solution via native js by that way but I want to know how can I do it via Lodash. 我已经通过本机js解决了该解决方案,但是我想知道如何通过Lodash来实现。
export function mapServerModelToStateValues(state, serverModel) {
let updatedState = {};
const serverModelKeyList = Object.keys(serverModel);
Object.keys(state).forEach(stateKey => {
serverModelKeyList.forEach(modelKey => {
if ( modelKey === stateKey ) {
updatedState[ stateKey ] = {
...state[ stateKey ],
value : serverModel[ modelKey ]
}
}
});
});
console.log(updatedState);
}
You can use _.mergeWith
lodash method and pass custom function. 您可以使用
_.mergeWith
lodash方法并传递自定义函数。
var a = {"beta":{"value":null,"error":null},"hamma":{"value":null,"error":null},"zerta":{"value":null,"error":null},"mozes":5} var b = {"beta":5,"hamma":2, "mozes": 123} _.mergeWith(a, b, function(aValue, bValue) { _.isPlainObject(aValue) ? aValue.value = bValue : aValue = bValue return aValue }) console.log(a)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
This will do the trick, notice that the result will have all the keys in the same value, error
format 这可以解决问题,请注意结果将使所有键的
value, error
相同value, error
格式
const A = { "beta": { "value": null, "error": null }, "hamma": { "value": null, "error": null }, "zerta": { "value": null, "error": null }, "mozes": 5 } const B = { "beta": 5, "hamma": 2 } const C = _.mapValues(A, (value, key) => { return { ...value, value: B[key] || value.value } }); console.log(C)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
这是一个遍历B数组并使用set更新值的解决方案:
_.each(updates, (value, key) => _.set(data, `${key}.value`, value))
Map through the _keys
from your second object to update the first object accordingly: 通过第二个对象的
_keys
映射,以相应地更新第一个对象:
// obj a const a = { beta: { value: null, error: null }, hamma: { value: null, error: null }, zerta: { value: null, error: null }, mozes: 5 }; // obj b const b = { beta: 5, hamma: 2 }; // map through the 'obj b' keys and update 'obj a' accordingly _.keys(b).map(key => { a[key].value = b[key]; }); // log obj a to the console console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Update: I would use the solution from @GruffBunny - the idea is similar to this one, but done much more elegantly using lodash. 更新:我将使用@GruffBunny的解决方案-这个想法与此类似,但是使用lodash可以做得更好。
You can use lodash#merge
and lodash#mapValues
to achieve this. 您可以使用
lodash#merge
和lodash#mapValues
实现此目的。
var result = _.merge({}, a, _.mapValues(b, value => ({ value })));
var a = { "beta" : { "value": null, "error" : null }, "hamma" : { "value": null, "error" : null }, "zerta" : { "value": null, "error" : null }, "mozes" : 5 }; var b = { "beta" : 5, "hamma" : 2 }; var result = _.merge({}, a, _.mapValues(b, value => ({ value }))); console.log(result);
.as-console-wrapper { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.