Reputation: 135
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 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:
23-26
only will show Beck and Candace>27
only will show JoeThank you for any answer.
Upvotes: 1
Views: 342
Reputation: 11848
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)))
Upvotes: 0
Reputation: 10662
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;
}
})
Upvotes: 0
Reputation: 247
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' ] }
]
Upvotes: 0
Reputation: 386578
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; }
Upvotes: 1
Reputation: 26
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"]
}
]
Upvotes: 0