使用 jQuery grep() 过滤 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21172289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:28:12  来源:igfitidea点击:

Filtering JSON array using jQuery grep()

jqueryarraysjsongrep

提问by RiddleMeThis

I've searched many examples on this site but can't seem to fit them into my needs. I just need to filter some JSON results using grep().

我在这个网站上搜索了很多例子,但似乎无法满足我的需求。我只需要使用grep().

Below is my JSON:

以下是我的 JSON:

var data = { "items": [
    {
        "id":       1,
        "category": "cat1"
    },
    {        
        "id":       2,
        "category": "cat2"
    },
    {
        "id":       3,
        "category": "cat1"
    }
]}


With the example above


用上面的例子

  • how would I return all items with the category of cat1?
  • how would I return all items with the category of cat1and idof 3 ?
  • 我将如何返回类别为 的所有项目cat1
  • 我如何将返回所有项目与类别cat1id3?

I know this isn't a great example but any help would be awesome! Thanks!

我知道这不是一个很好的例子,但任何帮助都会很棒!谢谢!

I have tried variations of the following

我尝试了以下变体

data.items = $.grep(data.items, function(element, index) {
    return element.id == 1;
    console.log(data.items);
});

回答by marko

var data = {
    "items": [{
        "id": 1,
        "category": "cat1"
    }, {
        "id": 2,
        "category": "cat2"
    }, {
        "id": 3,
        "category": "cat1"
    }]
};

var returnedData = $.grep(data.items, function (element, index) {
    return element.id == 1;
});


alert(returnedData[0].id + "  " + returnedData[0].category);

The returnedData is returning an array of objects, so you can access it by array index.

ReturnedData 返回一个对象数组,因此您可以通过数组索引访问它。

http://jsfiddle.net/wyfr8/913/

http://jsfiddle.net/wyfr8/913/

回答by Parth Raval

var data = {
  "items": [{
    "id": 1,
    "category": "cat1"
  }, {
    "id": 2,
    "category": "cat2"
  }, {
    "id": 3,
    "category": "cat1"
  }, {
    "id": 4,
    "category": "cat2"
  }, {
    "id": 5,
    "category": "cat1"
  }]
};
//Filters an array of numbers to include only numbers bigger then zero.
//Exact Data you want...
var returnedData = $.grep(data.items, function(element) {
  return element.category === "cat1" && element.id === 3;
}, false);
console.log(returnedData);
$('#id').text('Id is:-' + returnedData[0].id)
$('#category').text('Category is:-' + returnedData[0].category)
//Filter an array of numbers to include numbers that are not bigger than zero.
//Exact Data you don't want...
var returnedOppositeData = $.grep(data.items, function(element) {
  return element.category === "cat1";
}, true);
console.log(returnedOppositeData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id='id'></p>
<p id='category'></p>

The $.grep()method eliminates items from an array as necessary so that only remaining items carry a given search. The test is a function that is passed an array item and the index of the item within the array. Only if the test returns true will the item be in the result array.

$.grep()方法根据需要从数组中删除项目,以便只有剩余的项目进行给定的搜索。测试是一个函数,它传递一个数组项和数组中该项的索引。只有当测试返回 true 时,该项目才会出现在结果数组中。