lodash:深度合并对象
lodash: deep merge in objects
我想合并对象,来自这个对象:
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" }
}
更新:@ryeballar 的 解决方案有效,但我发现了问题,"children object" 包含与父对象相同的键名。
您可以将 mapValues(), and have a callback that returns each object with an omitted value
by using omit() and merge() 与 value
本身一起使用。
var objs = {
one: { description: "value", amount: 5, value: { identifier : "some text"} },
two: { description: "value", amount: 5, value: { identifier : "some text"} }
};
var result = _.mapValues(objs, i => _(i).omit('value').merge(i.value).value());
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>
更新
为了响应您的更新,您仍然可以使用上面的原始解决方案,在您使用 mapKeys() 将每个对象键从给定前缀(如果它存在于合并源。
var 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"} },
three: { description: "value", amount: 5, value: null },
four: { description: "value", amount: 5 }
}
function mergeFromKey(object, mergeKey, prefix) {
return _.mapValues(object, item =>
_(item).mapKeys((v, k) => (_.get(item[mergeKey], k)? prefix: '') + k)
.omit(mergeKey)
.merge(item[mergeKey])
.value()
);
}
var result = mergeFromKey(objs, 'value', 'original_');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>
我想合并对象,来自这个对象:
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" }
}
更新:@ryeballar 的 解决方案有效,但我发现了问题,"children object" 包含与父对象相同的键名。
您可以将 mapValues(), and have a callback that returns each object with an omitted value
by using omit() and merge() 与 value
本身一起使用。
var objs = {
one: { description: "value", amount: 5, value: { identifier : "some text"} },
two: { description: "value", amount: 5, value: { identifier : "some text"} }
};
var result = _.mapValues(objs, i => _(i).omit('value').merge(i.value).value());
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>
更新
为了响应您的更新,您仍然可以使用上面的原始解决方案,在您使用 mapKeys() 将每个对象键从给定前缀(如果它存在于合并源。
var 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"} },
three: { description: "value", amount: 5, value: null },
four: { description: "value", amount: 5 }
}
function mergeFromKey(object, mergeKey, prefix) {
return _.mapValues(object, item =>
_(item).mapKeys((v, k) => (_.get(item[mergeKey], k)? prefix: '') + k)
.omit(mergeKey)
.merge(item[mergeKey])
.value()
);
}
var result = mergeFromKey(objs, 'value', 'original_');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>