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()
.
下面是我的JSON:
var data = { "items": [
{
"id": 1,
"category": "cat1"
},
{
"id": 2,
"category": "cat2"
},
{
"id": 3,
"category": "cat1"
}
]}
使用上面的示例
- how would I return all items with the category of
cat1
? - 我如何退还类别为
cat1
和id
(共3个)的所有项目?
我知道这不是一个很好的例子,但任何帮助都会很棒!谢谢!
I have tried variations of the following
data.items = $.grep(data.items, function(element, index) {
return element.id == 1;
console.log(data.items);
});