我有以下脚本,我需要返回以下结果:
{
text: 'i love apples'
}, {
text: 'i eat my apple'
}, {
text: 'no apples on the table'
}
使用以下脚本我得到一个错误。
我做错了什么,如何解决(不使用 .match 的另一种解决方案也可以)?
注意:搜索值可能不同,因此脚本应该是可重用的。
let data = [{
text: 'i love apples'
}, {
text: 'i eat my apple'
}, {
text: 'no apples on the table'
},{
text: 'i love oranges'
}];
let search = 'apple'
let filter = data.filter(item=>{ return item.match(search)})
console.log(filter)
最佳答案
使用includes
相反,并确保在迭代中引用该属性。
let data = [{
text: 'i love apples'
}, {
text: 'i eat my apple'
}, {
text: 'no apples on the table'
}, {
text: 'i love oranges'
}];
let search = 'apple';
let filter = data.filter(item => item.text.includes(search))
console.log(filter)
关于javascript - 不能在 js 中使用 string.match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46308065/