Palaniichuk Dmytro
Palaniichuk Dmytro

Reputation: 3173

Javascript remove and return new object with unnessary instanceof constructor

How to remove from an object unnessary instanceof constructor, an return new one without this instance. Is it possible to use reduce as one method.

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']
}

I want to return common without plugins auto, auto2 I need to use something like this

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

Upvotes: 0

Views: 32

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23545

Is this what you want ?

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);

Upvotes: 1

Related Questions