簡體   English   中英

Underscore.js數組過濾

[英]Underscore.js array filtration

我一直用這個拉出我的頭發 - 我有以下數組,繼續一個對象 - 它包含一個憑證數組(包含可能無數個對象)

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' } ] 
} ]  

使用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' }] 
    } ]  

所以使用下划線js,我基於先前的堆棧溢出線程( 使用underscore.js過濾數組 )編寫了以下內容

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

這將返回所有可見的優惠券 - 但是,我在此過程中失去了零售商。 最好的方法是什么? 我嘗試了很多不同的東西 - 也就是說,試圖用新的代替舊的voucheers數組 - 但它似乎不起作用。

謝謝,羅布

在零售商處使用_.map() ,這是一對一的映射。 在回調(每個零售商項目)內,使用_.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;
});

這將返回一個新數組,不會更改您的初始零售商數組。

如果您更喜歡_.reject() ,則必須相應地調整回調:

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

希望這可以幫助!

我發現了一篇博客文章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