简体   繁体   中英

Dynamic Multiple Variable Filter Function

Summary:
Thanks for reading! I'm stuck trying to figure out how to pass multiple variables (of unknown quantity) into an argument that is taking in an array of objects (of unknown quantity) In short: I want to filter out people that don't match all of the criteria listed from within another array (I'm also open to using rest instead of an array).

Code access:

This is awesome except its assuming I already know the params I'm seeking to filter by and the elements I'm seeking to look for associated params.

// Object Arrays:

let people = [
    {name: "Steve",  age: 27, country: "America"},
    {name: "Linda",  age: 23, country: "Germany"},
    {name: "Jimena", age: 29, country: "Spain"},
    {name: "Eric",   age: 24, country: "England"},
    {name: "Jacob",  age: 24, country: "America"},
];

let filters = [
    {country: "America"},
    {Age: 24}
];

// Function: (Attempt)

const filterMatch = {}
  
const filterThePeople = ((data, filters) => {
  
    filters.forEach(filter => {
    
        //==========get key and value of filter=====================
        for (const [key, value] of Object.entries(filter)) {
            filterMatch[key] = value
            return {key, value}
        }
      
    })

    data.forEach(person => {
        for (const [key2, value2] of Object.entries(data)) {
            
            return {key2, value2}
        }
    })

      
  })
  console.log(filterThePeople(people, filters))

I'm not sure whether to place the "data.forEach..." statement inside the for statement above... I was thinking of placing an if or switch statement saying to look through the array for each key and when it finds a matching key, then it will look at the value of each and if both keys and values match then += to a list... eventually to return the list to the function. (at least that's the solution I imagine in my head. I'm totally open to any and all alternative ways of accomplishing this!)

Filter People that match every filter.. I might add that the naming of the KEY is very important here. In your example you had Age as a capital later. This also allows you to construct filters like this:

let filters = [ {country: "America", age: 24} ];

 let people = [ { name: "Steve", age: 27, country: "America", }, { name: "Linda", age: 23, country: "Germany", }, { name: "Jimena", age: 29, country: "Spain", }, { name: "Eric", age: 24, country: "England", }, { name: "Jacob", age: 24, country: "America", }, ]; let filters = [ {country: "America"}, {age: 24} ]; let results = people.filter(p => filters.every(f=> Object.keys(f).every(k => p[k] === f[k])) ) console.log(results);

You can do something like this

 let people = [ {name: "Steve", age: 27, country: "America"}, {name: "Linda", age: 23, country: "Germany"}, {name: "Jimena", age: 29, country: "Spain"}, {name: "Eric", age: 24, country: "England"}, {name: "Jacob", age: 24, country: "America"}, ]; let filters = [ {country: "America"}, {age: 24} ]; const filtered = people.filter(p => filters.every(f => { const [key] = Object.keys(f) return p[key] === f[key] })) console.log(filtered)

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