如果包含某些字符串,如何过滤数组值

时间:2017-11-21 01:03:38

标签: javascript arrow-functions filterfunction

我有一个看起来像这样的数组

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




2 个答案:

答案 0 :(得分:1)

请尝试Array.prototype.indexOf

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)