删除不属于或继承的财产
Removing properties which are not owned or inherited
这是我的代码。
function Material() {
this.id = null;
}
function Box() {
// Inherit Material props
Material.call(this)
// Define own props
this.width = null;
this.height = null;
this.weight = null;
}
var box = new Box();
// Setting values to Box props
box.id = 12;
box.width = 250;
box.height = 350;
box.weight = '10kg';
// Not its property
box.color = 'red';
// TODO: Passing box variable via function which will return new object of Box without color property
console.log(box);
// {
// 'id': 12,
// 'width': 250,
// 'height': 350,
// 'weight': '10kg',
// }
所以基本上在某些时候我想摆脱在盒子对象上设置的颜色 属性(这不是盒子 Class 的 属性)。
我不想单独删除此类属性,因为可能有很多。我无法找出要删除的属性。但我只知道要保留哪些属性(它自己的和继承的)。
我这里用的是lodash。我尝试 _.forIn
和 _.defaults
但没有帮助。
谢谢
尝试these:
var diff = _.difference(_.keys(box), _.keys(new Box()));
var clear = _.omit(box, diff);
console.log(clear);
clear
- 是 box
对象的副本,具有初始属性集合
您可以创建一个虚拟 Box
对象,遍历对象的属性并检查虚拟对象是否包含它:
function Material() {
this.id = null;
}
function Box() {
// Inherit Material props
Material.call(this)
// Define own props
this.width = null;
this.height = null;
this.weight = null;
}
function removeAdditionalProperties(obj) {
var dummy = new Box();
for (var key in obj) {
if (!dummy.hasOwnProperty(key)) {
delete obj[key]; // Or whatever you want to do with that prop
}
}
}
var box = new Box();
// Setting values to Box props
box.id = 12;
box.width = 250;
box.height = 350;
box.weight = '10kg';
// Not its property
box.color = 'red';
removeAdditionalProperties(box);
console.log(box);
这是我的代码。
function Material() {
this.id = null;
}
function Box() {
// Inherit Material props
Material.call(this)
// Define own props
this.width = null;
this.height = null;
this.weight = null;
}
var box = new Box();
// Setting values to Box props
box.id = 12;
box.width = 250;
box.height = 350;
box.weight = '10kg';
// Not its property
box.color = 'red';
// TODO: Passing box variable via function which will return new object of Box without color property
console.log(box);
// {
// 'id': 12,
// 'width': 250,
// 'height': 350,
// 'weight': '10kg',
// }
所以基本上在某些时候我想摆脱在盒子对象上设置的颜色 属性(这不是盒子 Class 的 属性)。
我不想单独删除此类属性,因为可能有很多。我无法找出要删除的属性。但我只知道要保留哪些属性(它自己的和继承的)。
我这里用的是lodash。我尝试 _.forIn
和 _.defaults
但没有帮助。
谢谢
尝试these:
var diff = _.difference(_.keys(box), _.keys(new Box()));
var clear = _.omit(box, diff);
console.log(clear);
clear
- 是 box
对象的副本,具有初始属性集合
您可以创建一个虚拟 Box
对象,遍历对象的属性并检查虚拟对象是否包含它:
function Material() {
this.id = null;
}
function Box() {
// Inherit Material props
Material.call(this)
// Define own props
this.width = null;
this.height = null;
this.weight = null;
}
function removeAdditionalProperties(obj) {
var dummy = new Box();
for (var key in obj) {
if (!dummy.hasOwnProperty(key)) {
delete obj[key]; // Or whatever you want to do with that prop
}
}
}
var box = new Box();
// Setting values to Box props
box.id = 12;
box.width = 250;
box.height = 350;
box.weight = '10kg';
// Not its property
box.color = 'red';
removeAdditionalProperties(box);
console.log(box);