Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  67] [ 7]  / answers: 1 / hits: 11127  / 10 Years ago, tue, may 20, 2014, 12:00:00

I want to filter price range between 250 to 800 in JSON Data



My JSON Data :



var data = [{
name: Lenovo Thinkpad 41A4298,
Price: 200
}, {
name: Lenovo Thinkpad 41A2222,
Price: 200
}, {
name: Lenovo Thinkpad 41Awww33,
Price: 6700
}, {
name: Lenovo Thinkpad 41A424448,
Price: 600
}, {
name: Lenovo Thinkpad 41A429rr8,
Price: 4200
}, {
name: Lenovo Thinkpad 41A429ff8,
Price: 2200
}, {
name: Lenovo Thinkpad 41A429ss8,
Price: 200
}, {
name: Lenovo Thinkpad 41A429sg8,
Price: 500
}];


How to do in Javascript or jquery for Price range filter with All browser compatibility



I am try this code :



newdata=data.filter(function (el) {
return el.Price >= 250 &&
e1.Price <=800;
});

More From » jquery

 Answers
1

You can make use of the filter method, which will create a new array with all the elements that passes the condition.



data.filter(function(x){ return x.Price >= 250 && x.Price <= 800});


If you want to use filter method in every browser you could add the polyfill method (see link) in your code.






Another option with basic javascript would be:



Looping trough the array with a simple for loop and test on the price range.



for(var i=0, length=data.length; i<length; i++){
var current = data[i];
if(current.Price >= 250 && current.Price <= 800){
//INSERT CODE HERE
}
}

[#45190] Monday, May 19, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kirstendevanb

Total Points: 585
Total Questions: 93
Total Answers: 94

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;