[英]lodash: deep copy object but not all properties
Is there any way to copy an object with lodash, but not all properties. 有没有办法用lodash复制对象,但不是所有属性。 The only way I know is manually copying it property by property 我知道的唯一方法是按属性手动复制属性
wanted eg: 想要例如:
var obj = {
a: 'name',
b: [1,2,3],
c: {
z: 'surname',
x: []
},
d: {
y: 'surname2',
w: []
}
};
and the result be like 结果就像
var copy_obj = {
b: [1,2,3],
c: {
z: 'surname',
x: []
}
};
Edit: I finally opted for: 编辑:我最后选择了:
var blacklist = ['a','d'];
_.cloneDeep(_.omit(obj, blacklist));
The omit
serves almost this exact purpose: omit
几乎就是为了这个目的:
_.cloneDeep(_.omit(obj, blacklist));
Fiddle here: https://jsfiddle.net/c639m9L2/ 在这里小提琴: https : //jsfiddle.net/c639m9L2/
你可以使用选择功能:
_.pick(obj, 'b', 'c')
您可以使用第二个参数JSON.stringify
来执行此操作。
JSON.parse(JSON.stringify(obj, ['b', 'c']))
You can use a combination of assign and pick 您可以使用assign和pick的组合
Object.assign(copy_obj, _.pick(obj, ['b', 'c']));
In this way if copy_obj has other properties you don't override them. 这样,如果copy_obj具有其他属性,则不会覆盖它们。
var blacklist = ['a','d'];
_.cloneDeep(_.omit(obj, blacklist));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.