代码:
filteredList() {
// TODO: 请补充代码
return this.postList.filter(item => { //返回过滤完的新数组
return item.title.search(this.search) !== -1 //search搜索是否存在关键字
})
},
知识点:
1.过滤器array.filter()
用于筛选数组
let arr = arr.filter(function(currentValue, index, arr) {
return //满足条件直接return,用新数组接收
})
2.string.search(regexp)
法用于在 String 对象中执行正则表达式的搜索,寻找匹配项
如果匹配成功,则返回正则表达式在字符串中首次匹配的索引;否则,返回 -1
。
const paragraph = "I think Ruth's dog is cuter than your dog!";
const regex = /[^\w\s']/g;
console.log(paragraph.search(regex)); //41
//若参数不是正则,会隐式转换为正则