简体   繁体   English

如何在 JSON 数据中过滤价格范围:Javascript 或 jQuery

[英]How to Filter Price range in JSON Data : Javascript or jQuery

I want to filter price range between 250 to 800 in JSON Data我想在 JSON 数据中过滤 250 到 800 之间的价格范围

My JSON Data :我的 JSON 数据:

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如何在 Javascript 或 jquery 中执行与所有浏览器兼容的价格范围过滤器

I am try this code :我正在尝试这个代码:

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

You can make use of the filter method, which will create a new array with all the elements that passes the condition.您可以使用filter方法,该方法将创建一个包含所有满足条件的元素的新数组。

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.如果你想在每个浏览器中使用 filter 方法,你可以在你的代码中添加 polyfill 方法(见链接)。


Another option with basic javascript would be:使用基本 javascript 的另一个选择是:

Looping trough the array with a simple for loop and test on the price range.使用简单的 for 循环遍历数组并测试价格范围。

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.正如您感到难过一样,请为 xbrowser 使用.filter和 shims。 jQuery should have that functionality built-in. jQuery 应该内置该功能。

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 .它使用一个名为filter的 JavaScript Native 函数。

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()使用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.您可以通过在数组中使用 filter() 方法来过滤值,该方法将返回一个新的过滤数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM