Issue
I have the following array of objects:
[
{
"id" : 1,
"pricePerDay" : 50,
},
{
"id" : 2,
"pricePerDay" : 70
}
]
Based on the user input from a i want to filter either ASC or DESC on the pricePerday. Obviously this would work with:
this.products.sort((a, b) => parseFloat(b.pricePerDay) - parseFloat(a.pricePerDay));
BUT i have the variable filterType
which now contains the string 'pricePerDay'
. How can I use this variable to search the array of objects to properties that have the same key and sort this arry based on that key?
Solution
this.products.sort((a, b) => parseFloat(b[filterType]) - parseFloat(a[filterType]));
Does this answer your question?
Answered By – Sanket Shah
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0