简体   繁体   English

过滤JSON数组

[英]Filtering JSON Array

I need to add filter option to my grid.I use Fixed Data Table.Here is simple filtering example with that grid. 我需要在网格中添加过滤器选项,使用固定数据表,这是该网格的简单过滤示例。

https://github.com/facebook/fixed-data-table/blob/master/examples/old/FilterExample.js https://github.com/facebook/fixed-data-table/blob/master/examples/old/FilterExample.js

This example filter the Json array only by first name.But I need to filter by all of the objects in JSON Array. 这个例子只按名字过滤Json数组,但是我需要按JSON Array中的所有对象过滤。 For example may JSON array is here: 例如,可能是JSON数组在这里:

 {"id":7,"first_name":"Sarah","last_name":"Hottie",
"country":"Sweden","salary":12000},

{"id":9,"first_name":"Mary","last_name":"Parah",
"country":"Argentina","salary":10000}

When I write "arah" to the general input filter value.I need to show both of the two elements of array.Because "id:7" first name (Sarah) and "id:9" last name (Parah) include my filter value ("arah"). 当我在常规输入过滤器值中写入“ arah”时,我需要显示数组的两个元素。由于“ id:7”的名字(Sarah)和“ id:9”的名字(Parah)包含了我的过滤器值(“ arah”)。

If the country value of the another element of JSON array include "arah" I need to show that too. 如果JSON数组的另一个元素的国家/地区值包含“ arah”,我也需要显示。

So I need to filter the JSON array by all of the values it include. 因此,我需要通过它包含的所有值来过滤JSON数组。 What do you suggest? 你有什么建议?

You can utilize the filter prototype of the array. 您可以利用数组的过滤器原型。 It will be something like this: 将会是这样的:

var arr = [ {"id":7,"first_name":"Sarah","last_name":"Hottie",
"country":"Sweden","salary":12000}, {"id":9,"first_name":"Mary","last_name":"Parah","country":"Argentina","salary":10000}]


var runFilter = function(arr,searchKey) {

    var filterFn = function(obj) { 

      // Iterate the obj for each key. 
      for (var k in obj) {
        if (typeof obj[k] == "string" && obj[k].indexOf(searchKey) >= 0) {
          return true;
        }
      }
     }


      return arr.filter(filterFn);
    }

var filteredArr = runFilter(arr,'arah')

You can find the filter function in line 45 of the example code. 您可以在示例代码的第45行中找到过滤器功能。 It is 它是

return row['firstName'].toLowerCase().indexOf(filterBy.toLowerCase()) >= 0

If you want to look into every part of an Object, you can use a for...in loop: 如果要查看对象的每个部分,可以使用for...in循环:

for(var key in row){
    if((row[key] + "").indexOf(filterBy) > -1){
        return true;
    }
}
return false;

Replace line 45 with the code above and you should be fine. 用上面的代码替换第45行,就可以了。

I suggest to use Array#filter in combination with Array#some and a check of the type. 我建议结合使用Array#filter和Array#some和检查类型。

 var data = [{ "id": 7, "first_name": "Sarah", "last_name": "Hottie", "country": "Sweden", "salary": 12000 }, { "id": 9, "first_name": "Mary", "last_name": "Parah", "country": "Argentina", "salary": 10000 }], search = 'arah', result = data.filter(function (a) { return Object.keys(a).some(function (k) { if (typeof a[k] === 'string' && ~a[k].indexOf(search)) { return true; } if (typeof a[k] === 'number' && ~a[k] === search) { return true; } }); }); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 

Try This : 尝试这个 :

<script type="text/javascript">
        var arr = [ {"id":7,"first_name":"Sarah","last_name":"Hottie","country":"Sweden","salary":12000}, 
                  {"id":8,"first_name":"Mary","last_name":"Parah","country":"Argentina","salary":10000},
                  {"id":9,"first_name":"Gold","last_name":"sonam","country":"India","salary":15000}];
         var filterKey = 'arah';  
         function findJsonString(arr,filterKey){
               var result = [];           
               for (var i = arr.length - 1; i >= 0; i--) {
                  var part1 = arr[i].first_name.indexOf(filterKey);
                  var part2 = arr[i].last_name.indexOf(filterKey);
                  // console.log(arr[i]);
                  // console.log(' part 1 : ' + part1 + ' part 2 : ' + part2);
                  if(part1 != -1 || part2 != -1)
                  {                          
                      result[+i] = arr[i]; 
                      // OR result.push(arr[i]);                        
                  }
               }
               return result;
         }         
         console.log(findJsonString(arr,filterKey));
      </script>

OUTPUT : 输出:

[Object { id=7, first_name="Sarah", last_name="Hottie", more...}, Object { id=8, first_name="Mary", last_name="Parah", more...}]

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

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