一.数组findIndex方法
findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
let number = this.items.findIndex(item=>item===this.content)
if(number < 0){
this.items.push(this.content)
} else {
alert(‘不能重复添加’)
}
二.数组forEach方法
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
foreach方法判断重复
if(this.items.length>0){
this.items.forEach(item=>{
if(item===this.content){
alert(“不能重复添加”)
}else{
console.log(this.content)
this.items.push(this.content)
console.log(this.items)
}
})
}else{
this.items.push(this.content)
}