在jquery中过滤一个对象 [英] filter an object in jquery

查看:140
本文介绍了在jquery中过滤一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(this).find('li')。每个(函数(){
var $ itm = $(this);
localproducts.push({$ b $'dataid':$ itm.attr('data-id'),
'datapackage':$ itm.attr('data-package'),
'packageid':($ itm.children('.packidid').text())
});
});

现在我要过滤创建的对象localproducts,例如。我想保存packageid等于3的所有项目的dataid。我认为这可以用数组过滤器来完成,但不知道如何。任何帮助?谢谢!

解决方案使用过滤器

  var matching = $(localproducts).filter(function(){
return this.packageid == 3;
});






使用 code $>

$ $ p $ var dataids = new Array();
$ b $(localproducts).each(function(){
if(this.packageid == 3)dataids.push(this.dataid);
});






示例: http://jsfiddle.net/hunter/tqTDQ/


I have an object which I have created as follows:

$(this).find('li').each(function(){
                var $itm = $(this);
                localproducts.push({
                    'dataid' : $itm.attr('data-id'), 
                    'datapackage' : $itm.attr('data-package'), 
                    'packageid' : ($itm.children('.packageid').text())
            });
        });

now I want to filter the created object localproducts, eg. I want to save all the dataids of items whose packageid is equal to 3 for example. I think it can be done with array filter but not sure how. Any help? thanks!

解决方案

using filter

var matching = $(localproducts).filter(function(){  
    return this.packageid == 3;
});


using each

var dataids = new Array();

$(localproducts).each(function(){  
    if (this.packageid == 3) dataids.push(this.dataid);
});


Example: http://jsfiddle.net/hunter/tqTDQ/

这篇关于在jquery中过滤一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆