我已經拉出我的頭髮與此一 - 我有以下陣列,持續的對象 - 它包含一個憑證陣列(包含對象的可能的無限數量)Underscore.js陣列過濾
var retailer = [ { _id: 52000c,
address: 'bla bla bla',
email: '[email protected]',
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,我試圖用隱藏的屬性(hidden == true)過濾掉那些憑證對象 - 所以我最終得到了以下內容,這樣我只能得到可見的憑證(隱藏==假)
var retailer = [ { _id: 52000c,
address: 'bla bla bla',
email: '[email protected]',
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,我寫了下面的ba sed的在以前堆棧溢出線程(Filtering array with underscore.js)
var visibleVouchers = _(retailer[0].vouchers).filter(function (x) { return !x.hidden;});
而這將返回所有可見的憑證 - 但是,我失去了零售商的過程中。什麼是最好的方法來做到這一點?我已經嘗試了很多不同的東西 - 例如,試圖用新的替代舊的voucheers陣列 - 但它似乎並不奏效。
謝謝, 羅布
真棒 - 所以這保留了原產地,並創造了一個新的我需要的一切。謝謝 :) – Rob