1

I have the scenario, with provided example JSON data:

[{
   "name": "Joe",
   "age": "28",
   "hobby": ["Reading", "Chilling", "Cycling"]
},
{
   "name": "Beck",
   "age": "25",
   "hobby": ["Drinking", "Cycling"]
},
{
   "name": "Candace",
   "age": "24",
   "hobby": ["Reading", "Singing"]
}]

Let's say the array above are shown in some list in web page.

I have 2 (two) search dropdown with multiple selection to search name and age.

  • For name, let's say it shows all distinct hobbies (Reading, Chilling, Cycling, Drinking, Singing).
  • For age, let's say it have 2 options for age range: 23-26 and > 27.

For searching hobby, my code is (in ReactJS):

filteredPersons = this.state.personList.filter( person =>{
  return person.hobby.some(v=> filterHobby.indexOf(v) !== -1)
});

which is:

  • personList is list of array from JSON above;
  • filterHobby is list of array from multiple select, ex: if filterHobby = ["Reading","Singing"], it will show Joe and Candace.

The question is, how can I search for age with multiple selection?

Expected result:

  • Choose 23-26 only will show Beck and Candace
  • Choose >27 only will show Joe
  • Choose both will show Beck, Candace and Joe

Thank you for any answer.

1
  • In a first step map the selected age range to an appropriate filter function (I'd suggest not doing magic here, a simple switch or similar... [readability; can be extracted/*magic-ified* later if it gets too complex]), then apply that filter function similar to what you do with filterHobby. Commented Feb 14, 2020 at 9:27

5 Answers 5

1

You could take an object with all filters and another for keeping function for getting the ages of the objects.

var data = [{ name: "Joe", age: "28", hobby: ["Reading", "Chilling", "Cycling"] }, { name: "Beck", age: "25", hobby: ["Drinking", "Cycling"] }, { name: "Candace", age: "24", hobby: ["Reading", "Singing"] }],
    functions = {
        '23-26': ({ age }) => age >= 23 && age <= 26,
        '>27': ({ age }) => age > 27
    },
    filters = {
        hobby:  ["Reading", "Cycling"],                
        age: ['23-26']
    },
    result = data.filter(o =>  
        filters.hobby.some(v => o.hobby.includes(v)) &&
        filters.age.some(fn => functions[fn](o))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

You need use filter function for filtering an array.

We have an array with persons:

const persons = [
    {
       "name": "Joe",
       "age": "28",
       "hobby": ["Reading", "Chilling", "Cycling"]
    },
    {
       "name": "Beck",
       "age": "25",
       "hobby": ["Drinking", "Cycling"]
    },
    {
       "name": "Candace",
       "age": "24",
       "hobby": ["Reading", "Singing"]
    }
]

And then we will filter that array:

persons.filter((person) => {
    return person.age > 27;
})

This code returns us Joe's info:

[
  {
    "name": "Joe",
    "age": "28",
    "hobby": ["Reading", "Chilling", "Cycling"]
  }
]

Comments

0

For search person by its age, you can make an array of a selected age like this.

let ages = ['23','24','25'];

code like this

persons.filter(item=> {return ages.indexOf(item.age)!== -1 })

this return person array which are matches in ages array

output:

[
  { name: 'Beck', age: '25', hobby: [ 'Drinking', 'Cycling' ] },
  { name: 'Candace', age: '24', hobby: [ 'Reading', 'Singing' ] }
]

Comments

0
ageFilters = ['23-26','>27']
selectedFilter = '23-26';

filteredPersons = this.state.persons.filter(({age}) => {
 const _age = parseInt(age);
 if(selectedFilter === '23-26') {             // "23-26"
  return (_age>=23) && (_age<=26);
 } else if(selectedFilter === '>27') {                          // "27"
  return (_age>27);
 } else {
  return true;
 }
})

4 Comments

my dropdown value will be single array (like filterHobby described above) and i actually confused the value for each option. maybe like: filterAge = ["23-26", ">27"] (?)
in that array one value will be selected by the user right? use that as the condition for checking.
sorry, single array with multiple value, the selectedFilter can be multiple.
Okay, in that case, you can use nina's solution...Even if I edit my answer my approach will be similar.
0

You may create additional function ageComparer for convenience

const ageComparer = (ageRange, age) => (ageRange === '23-26' ? (age >= 23 && age <= 26) : age >=27)

And then utilize it during filtering

// ageFilter will be taken from drom down
const ageFilter = ['23-26'];

person.filter(p => p.hobby.some(v=> filterHobby.indexOf(v) !== -1) && ageFilter.some(a => ageComparer(a, +p.age)))

2 Comments

the ageFilter can be multiple (array)
Sure! Updated the answer for ageFilter to be an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.