简体   繁体   English

Underscore.js数组过滤

[英]Underscore.js array filtration

I've been pulling my hair out with this one - I have the following array, continuing an object - which contains a vouchers array (containing a possible infinite number of objects) 我一直用这个拉出我的头发 - 我有以下数组,继续一个对象 - 它包含一个凭证数组(包含可能无数个对象)

var retailer = [ { _id: 52000c,
    address: 'bla bla bla',
    email: 'test@emailaddress',
    img: 'http://bla.jpg',
    intro: ' hello',
    strapLine: 'goodbye',
    tel: '0000 0000000',
    title: 'YE OLDE SHOPPE',
    website: 'http://',
    vouchers: 
     [ { _id: 523d003,
         barcode: false,
         description: 'blah',
         endTime: '20 December 2013',
         hidden: true,
         redemptionCode: 'redemptionCode',
         smallPrint: 'blah.',
         startTime: 'Today',
         title: 'blahbla' },
       { _id: 523de3,
         barcode: false,
         description: 'blah',
         endTime: '20 December 2013',
         hidden: true,
         redemptionCode: 'redemptionCode',
         smallPrint: 'blah.',
         startTime: 'Today',
         title: 'blahbla' },
        { _id: 523dr,
         barcode: false,
         description: 'blah',
         endTime: '20 December 2013',
         hidden: false,
         redemptionCode: 'redemptionCode',
         smallPrint: 'blah.',
         startTime: 'Today',
         title: 'blahbla' } ] 
} ]  

Using underscore.js, I'm trying to filter out those voucher objects with a property of hidden (hidden == true) - so I end up with the following, so that I only end up with those vouchers which are visible (hidden == false) 使用underscore.js,我试图用隐藏属性(隐藏== true)过滤掉那些凭证对象 - 所以我最终得到以下内容,以便我最终得到那些可见的凭证(隐藏= = false)

var retailer = [ { _id: 52000c,
        address: 'bla bla bla',
        email: 'test@emailaddress',
        img: 'http://bla.jpg',
        intro: ' hello',
        strapLine: 'goodbye',
        tel: '0000 0000000',
        title: 'YE OLDE SHOPPE',
        website: 'http://',
        vouchers: 
         [{ _id: 523dr,
             barcode: false,
             description: 'blah',
             endTime: '20 December 2013',
             hidden: false,
             redemptionCode: 'redemptionCode',
             smallPrint: 'blah.',
             startTime: 'Today',
             title: 'blahbla' }] 
    } ]  

So using underscore js, I wrote the following based on a previous stack overflow thread ( Filtering array with underscore.js ) 所以使用下划线js,我基于先前的堆栈溢出线程( 使用underscore.js过滤数组 )编写了以下内容

var visibleVouchers = _(retailer[0].vouchers).filter(function (x) { return !x.hidden;});

And this returns all the visible vouchers - however, I lose the retailer in the process. 这将返回所有可见的优惠券 - 但是,我在此过程中失去了零售商。 What would be the best way to do this? 最好的方法是什么? I've tried lots of different things - ie, trying to replace the old voucheers array with the new one - but it doesn't seem to work. 我尝试了很多不同的东西 - 也就是说,试图用新的代替旧的voucheers数组 - 但它似乎不起作用。

Thanks, Rob 谢谢,罗布

Use _.map() on retailers, which is a one-to-one mapping. 在零售商处使用_.map() ,这是一对一的映射。 Inside the callback (each retailer item), filter out vouchers using _.filter() (or _.reject() , depending on your feeling). 在回调(每个零售商项目)内,使用_.filter() (或_.reject() ,根据您的感觉过滤掉凭证)。

var arrRetailers = _.map(retailers, function(retailer) {
  var item = _.extend({}, retailer);
  item.vouchers = _.filter(retailer.vouchers, function(voucher) {
    return !voucher.hidden;
  }) || []; //in case of there is no "visible" voucher
  return item;
});

This returns a new array and do not change your initial retailers array. 这将返回一个新数组,不会更改您的初始零售商数组。

If you prefer _.reject() , your callback must be adapted accordingly: 如果您更喜欢_.reject() ,则必须相应地调整回调:

_.reject(retailer.vouchers, function(voucher) {
    return voucher.hidden; //note there is no exclamation mark
}) || [];

Hope this helps! 希望这可以帮助!

I found a blog post http://www.untitleddesigns.com/2011/javascript-replace-array-contents/ which seems to answer my question - it does work, although I'm unfamiliar with prototypes - so don't quite know why its working. 我发现了一篇博客文章http://www.untitleddesigns.com/2011/javascript-replace-array-contents/似乎回答了我的问题 - 它确实有效,虽然我对原型不熟悉 - 所以不太了解为什么它的工作。

var visibleVouchers = _(retailer[0].vouchers).filter(function (x) { return !x.hidden;});
retailer[0].vouchers.length = 0;
Array.prototype.push.apply(retailer[0].vouchers, visibleVouchers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM