简体   繁体   中英

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?

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

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

like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM