封装一个函数,在数组中查找相同值,删除并返回之后的数组
这是一道面试题,有多种方法,在这里总结一下
forEach、some、findIndex、filter、map这些数组的方法,都会对数组的每一项进行遍历并执行相关的操作
1. forEach()方法
该方法用于调用数组的每一个元素,并将元素返回给数组(改变原始数组)
export default{
data(){
return {
list: [
{
id: ''
}
]
}
}
}
methods: {
delValue(id){
var _that = this;
_that.list.forEach((item,index) => {
if(item.id == id){
_that.list.splice(index,1);
}