使用match方法对字符串模糊查询
需要查询的原有数据:
data1: [
{
key: 1,
name: '第一'
},
{
key: 2,
name: 'ssss'
},
{
key: 3,
name: '12121'
},
{
key: 4,
name: '####'
},
{
key: 5,
name: '!!**'
}
查询数据
查询之后返回的数据
直接上代码:
hhh() {
console.log(this.data2)
var reg = new RegExp(this.title, 'i')
const arr = []
this.data1.forEach((item) => {
if (item.name.match(reg) !== null) {
arr.push(item)
}
})
this.data2 = arr
const deWeightThree = () => {
const map = new Map()
for (const item of this.data2) {
if (!map.has(item.key)) {
map.set(item.key, item)
}
}
return [...map.values()]
}
this.data2 = deWeightThree()
console.log(this.data2)
详细学习地址: