我有一个看起来像这样的数组
if( ptr != nullptr && ptr->predicate())
我希望过滤掉任何不包含“免费”的价值。在其中,我尝试了长度,但它没有工作,有人可以向我解释为什么?
[ { day: 'Friday', time: '14-16 Free' },
{ day: 'Friday', time: '16-18 Free' },
{ day: 'Friday', time: '18-20 Free' },
{ day: 'Friday', time: '20-22 Fully booked' } ]

答案 0 :(得分:1)
let data = [
{ day: 'Friday', time: '14-16 Free' },
{ day: 'Friday', time: '16-18 Free' },
{ day: 'Friday', time: '18-20 Free' },
{ day: 'Friday', time: '20-22 Fully booked' }
];
let result = data.filter((e) => e.time.indexOf('Free') === -1);
console.log(result); // [{day: "Friday", time: "20-22 Fully booked"}]
Plunker是here。
答案 1 :(得分:1)
使用ES6
' s includes()
:
var array = [ { day: 'Friday', time: '14-16 Free' },
{ day: 'Friday', time: '16-18 Free' },
{ day: 'Friday', time: '18-20 Free' },
{ day: 'Friday', time: '20-22 Fully booked' } ];
var res = array.filter((e) => (!(e.time.includes('Free'))));
console.log(res)