本文介绍了对象的JavaScript滤波器阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的数组,我想知道搜索它的最佳方式。考虑下面的例子我怎么可以搜索名=乔和年龄< 30?有什么jQuery的可以帮助或我蛮力这个搜索自己?

  VAR名=新的Array();
VAR对象= {名:乔,年龄:20,电子邮件:[email protected]};names.push(对象);
对象= {名字:迈克,年龄:50,电子邮件:[email protected]};
names.push(对象);
对象= {名:乔,年龄:45,电子邮件:[email protected]};
names.push(对象);


解决方案

您可以使用:

  VAR found_names = $ .grep(名称,功能(五){
    返回v.name ===乔&放大器;&安培; v.age< 30;
});

DEMO:

I have an array of objects and I'm wondering the best way to search it. Given the below example how can I search for name = "Joe" and age < 30? Is there anything jQuery can help with or do I have to brute force this search myself?

var names = new Array();
var object = { name : "Joe", age:20, email: "[email protected]"};

names.push(object);
object = { name : "Mike", age:50, email: "[email protected]"};
names.push(object);
object = { name : "Joe", age:45, email: "[email protected]"};
names.push(object);
解决方案

You may use jQuery.grep():

var found_names = $.grep(names, function(v) {
    return v.name === "Joe" && v.age < 30;
});

DEMO: http://jsfiddle.net/ejPV4/

这篇关于对象的JavaScript滤波器阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 16:04