JavaScript删除并返回带有不必要的instanceof构造函数的新对象

时间:2018-06-20 13:16:47

标签: javascript arrays object ecmascript-6

如何从不需要的instanceof构造函数中删除不带此实例的返回新构造函数。是否可以使用reduce作为一种方法。

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
var auto = new Car('Honda', 'Accord', 1998);
var auto2 = new Car('Syz', 'Accord', 1999);

const common = {
  name: 'name',
  plugins: [auto2, auto, 'ustom plugins']
}

我想返回common,不带插件auto, auto2 我需要使用类似的东西

const commonFiltered = Object.values(common).map(x => ({
      ...common,
      plugins: common2.plugins.filter(plugin => !(plugin instanceof Car))
}))

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

const auto = new Car('Honda', 'Accord', 1998);
const auto2 = new Car('Syz', 'Accord', 1999);

const common = {
  name: 'name',
  plugins: ['another custom plugin', auto2, auto, 'custom plugins']
};

// We make a copy
const commonFiltered = {
 ...common,
};

// We filter the plugins
commonFiltered.plugins = commonFiltered.plugins.filter(x => !(x instanceof Car));

console.log(commonFiltered);