Home > OS >  Filter proprites from array of objects
Filter proprites from array of objects

Time:10-12

I have this data coming from backend

[ { "total_spent": [ "greater_than", "less_than", "equal_to", "between" ] }, { "cart_value": [ "greater_than", "less_than", "equal_to", "between" ] }, { "shipping_price": [ "greater_than", "less_than", "equal_to", "between" ] }, { "product": [ "in" ] }, { "customer_type": [ "equal_to" ] } ]

And I am trying only to get total_spent and product and delete the rest.

I tried the following but I think it applies to objects.

filterConfi() {
      return ["cart_value", "shipping_price"].forEach(
         e => delete this.config.segmentationConfiguration[e]
      );
    },

The final result should look something like this

[ { "total_spent": [ "greater_than", "less_than", "equal_to", "between" ] }, { "product": [ "in" ] }, { "customer_type": [ "equal_to" ] } ]

CodePudding user response:

const arr = [ { "total_spent": [ "greater_than", "less_than", "equal_to", "between" ] }, { "cart_value": [ "greater_than", "less_than", "equal_to", "between" ] }, { "shipping_price": [ "greater_than", "less_than", "equal_to", "between" ] }, { "product": [ "in" ] }, { "customer_type": [ "equal_to" ] } ]

console.log(arr.filter(x=> !!x.total_spent || !!x.product))
  • Related