如何根据多个值搜索数组中的jQuery? [英] How jQuery in an array can be searched based on multiple values?
本文介绍了如何根据多个值搜索数组中的jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何根据多个值搜索数组中的jQuery?
例如,我按颜色和os搜索
感谢
var products = [
{ id: 1, title: Apple iPhone 4, os: ios, 颜色: red}
,{ id: 2, title: Blac80 Bold, os: 其他, color: blue}
,{ id: < span class =code-string> 12, title: Black 0, os: 其他, color: red}
,{ id: 26, title: HTC s, os: android, color: blue}
,{< span class =code-string> id: 50, title: HTC HD7,< span class =code-string> os: android, color: red}
,{ id: 21, title: HTC z, os: android, color: blue}
,{ id: 11, title: HTC ion , os: android, color: red}
,{ id: 3,< span class =code-string> title: HTC W, os: android, color: blue}
,{ id: 6, title: Motorola, os: ios, color: blue}
,{ id: 14, title: Motorola DEFY, os: ios, color: red}
,{ id: 15, title: 诺基亚C7, os: ios, color: blue}
,{ id: < span class =code-string> 60, title: 诺基亚N8, os: ios, color: red}
,{ id: 70, title: 诺基亚X6, os: ios, 颜色: blue}
];
解决方案
以红色取出所有诺基亚N8:
.grep(products, function (n,i){
返回 n.color == red&& n.os == 诺基亚N8;
});
在匿名函数中,n是数组中的项目;而我是它的索引。换句话说,诺基亚N8红色实际上是指数11,所以我们可以很容易地说:
< br>
.grep(产品,函数(n,i){
if(i == 11){
return true;
}
});
How jQuery in an array can be searched based on multiple values?
For example, I search by color and os
Thank
var products = [
{"id":"1","title":"Apple iPhone 4","os":"ios","color":"red"}
,{"id":"2","title":"Blac80 Bold","os":"other","color":"blue"}
,{"id":"12","title":"Black 0","os":"other","color":"red"}
,{"id":"26","title":"HTC s","os":"android","color":"blue"}
,{"id":"50","title":"HTC HD7","os":"android","color":"red"}
,{"id":"21","title":"HTC z","os":"android","color":"blue"}
,{"id":"11","title":"HTC ion","os":"android","color":"red"}
,{"id":"3","title":"HTC W","os":"android","color":"blue"}
,{"id":"6","title":"Motorola","os":"ios","color":"blue"}
,{"id":"14","title":"Motorola DEFY","os":"ios","color":"red"}
,{"id":"15","title":"Nokia C7","os":"ios","color":"blue"}
,{"id":"60","title":"Nokia N8","os":"ios","color":"red"}
,{"id":"70","title":"Nokia X6","os":"ios","color":"blue"}
];
解决方案
To get all Nokia N8 in red:
.grep(products, function(n, i) { return n.color == "red" && n.os == "Nokia N8"; });
In the anonymous function, n is the item in the array; and i is its index. In other words, Nokia N8 red is actually at index 11, so we could have easily have said:
.grep(products, function(n, i) { if(i == 11) { return true; } });
这篇关于如何根据多个值搜索数组中的jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文