[英]Javascript remove object keys that are not in both objects with Lodash?
So, I have this two objects:所以,我有这两个对象:
const obj1 = {
foo: 'Foo',
bar: {
baz: 'Baz',
}
}
const obj2 = {
foo: 'Foo',
bar: {
baz: 'Baz',
zip: 'Zip'
},
}
I want to remove "zip" from obj2 since it's not in obj1.我想从 obj2 中删除“zip”,因为它不在 obj1 中。 Is there a way to do this with lodash?
有没有办法用 lodash 做到这一点?
EDIT: If a property is an object, it will be an object in both of them.编辑:如果一个属性是一个对象,它将是它们两个中的一个对象。 There can't be a string with the same key of an object in the other one.
在另一个字符串中不能有一个对象的相同键的字符串。
The use case is, I have a translation file and after some processing, I need to remove the ones that are no longer needed.用例是,我有一个翻译文件,经过一些处理后,我需要删除不再需要的文件。 So if I delete them in the original object (ie obj1) they have to be removed from the other ones (in this case, obj2.
因此,如果我在原始对象(即 obj1)中删除它们,则必须将它们从其他对象(在本例中为 obj2.
If you are open to using additional libraries, I can recommend looking into deep-diff :如果您愿意使用其他库,我建议您查看deep-diff :
const obj1 = { foo: 'Foo', bar: { baz: 'Baz', } } const obj2 = { foo: 'Foo', bar: { baz: 'Baz', zip: 'Zip' }, } DeepDiff.observableDiff(obj2, obj1, d => { DeepDiff.applyChange(obj2, obj1, d) }); console.log(obj1) console.log(obj2)
<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.