admin管理员组

文章数量:1277896

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 patibility

I am try this code :

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

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 patibility

I am try this code :

newdata=data.filter(function (el) {
return el.Price >= 250 &&
e1.Price <=800;
}); 
Share Improve this question edited May 20, 2014 at 7:55 Me7888 asked May 20, 2014 at 7:47 Me7888Me7888 1,2094 gold badges18 silver badges19 bronze badges 2
  • 1 Please post the code you have written in an attempt to solve this yourself. – Rory McCrossan Commented May 20, 2014 at 7:49
  • you have a typo in your snippet that breaks (3rd line, 2nd column) e1 should be el. Google for monospace fonts aimed for coders, 'Droid Sans Mono' or 'Inconsolata' are nice ones and display similar characters more distinctly (like: O, 0, 1, l, etc.) – public override Commented May 21, 2014 at 7:46
Add a ment  | 

7 Answers 7

Reset to default 5

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 
   }
}

Just as you sad it, use .filter and shims for xbrowser. jQuery should have that functionality built-in.

var filtered = data.filter(filter_callback);
//

Try this solution:

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

You can do the following :

var filteredData = data.filter(function(val,i,arr){
    return (val.Price >= 230) && (val.Price <= 800);
});

It uses a JavaScript Native function called filter.

you can do like this:

var filtered = new Array();
 $.each(data, function (index, item) {
    if(item.Price >= 250 || item.Price <=800)    
        filtered.push(item);
});
    console.log(filtered);

Working Fiddle

Working Demo

Use grep()

var t=$.grep(data,function(val,i){

   return val.Price >= 250 && val.Price <= 800;
});

You can filter values by using filter() method in array which will return a new filtered array.

const filteredValue = data.filter(items => {
      return items.Price >= 230 && items.Price <= 800
})

本文标签: How to Filter Price range in JSON DataJavascript or jQueryStack Overflow