Paul Wieland
Paul Wieland

Reputation: 815

JavaScript: how to iteratively filter an array?

I want to be able to filter an array with one or many conditions.

Consider this sample data & filter criteria:

var data = [
  {company: "Acme", fiscal_period: '01-2019', value: 10},
  {company: "Acme", fiscal_period: '02-2019', value: 15},
  {company: "Wonka", fiscal_period: '01-2019', value: 8},
  {company: "Wonka", fiscal_period: '02-2019', value: 11}
]

var filter = [{field: "company", value: "Acme"}]
console.log(myFilterFunction(data,filter));
// Desired output 
// [
//   {company: "Acme", fiscal_period: '01-2019', value: 10},
//   {company: "Acme", fiscal_period: '02-2019', value: 15}
// ]

// Now use two filters:
var filter = [{field: "company", value: "Acme"},{field: "fiscal_period", value: "01-2019"}]
console.log(myFilterFunction(data,filter);
// Desired output 
// [
//   {company: "Acme", fiscal_period: '01-2019', value: 10},
// ]

But how to write the myFilterFunction?

I know how to make it work with a static filter:

myFilterFunction = function(data,filter){

  return data.filter(function(el){
    return el[filter.field] === filter.value;
  });

}

console.log(myFilterFunction(data,{field: "company", value: "Acme"});

But how to make it work dynamically if I want to have multiple filters?

Upvotes: 2

Views: 161

Answers (2)

jonathan Heindl
jonathan Heindl

Reputation: 851

data.filter(el=>{
    for(let t in filterArray){
        if(el[t]!=filterArray[t]){
            return false;
        }
    }
    return true;
})

like this

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Pass multiple field data as an array and use Array#every method to check all filters which return true only if all return values are true.

myFilterFunction = function(data,filters){    
  return data.filter(function(el){
    return filters.every(filter => el[filter.field] === filter.value);
  });    
}

console.log(myFilterFunction(data,[{field: "company", value: "Acme"},{field: "value", value: 10}]));

var data = [{
    company: "Acme",
    fiscal_period: '01-2019',
    value: 10
  },
  {
    company: "Acme",
    fiscal_period: '02-2019',
    value: 15
  },
  {
    company: "Wonka",
    fiscal_period: '01-2019',
    value: 8
  },
  {
    company: "Wonka",
    fiscal_period: '01-2019',
    value: 11
  }
]

myFilterFunction = function(data, filters) {
  return data.filter(function(el) {
    return filters.every(filter => el[filter.field] === filter.value);
  });
}

console.log(myFilterFunction(data, [{
  field: "company",
  value: "Acme"
}, {
  field: "value",
  value: 10
}]));

With ES6 Destructuring parameter and arrow function.

myFilterFunction = (data,filters) => {    
  return data.filter(el => filters.every(({ field, value }) => el[field] === value));    
}

var data = [{
    company: "Acme",
    fiscal_period: '01-2019',
    value: 10
  },
  {
    company: "Acme",
    fiscal_period: '02-2019',
    value: 15
  },
  {
    company: "Wonka",
    fiscal_period: '01-2019',
    value: 8
  },
  {
    company: "Wonka",
    fiscal_period: '01-2019',
    value: 11
  }
]


    myFilterFunction = (data,filters) => {    
      return data.filter(el => filters.every(({ field, value }) => el[field] === value));    
    }

console.log(myFilterFunction(data, [{
  field: "company",
  value: "Acme"
}, {
  field: "value",
  value: 10
}]));

Upvotes: 2

Related Questions