js 判断一个元素值是否在二维数组中并删除该元素

技术 · 2023-03-24 · 129 人浏览

方法1:

let array = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c},{id:4,name:'d'}]
let id= 3;
var newArray = array.filter(function (item) {
  if(id != item.id ){
      return item
  }
})
console.log(newArray )

方法2:

let array = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c},{id:4,name:'d'}]
let id = 3;
array.forEach((item,index)=>{
  if(id == item.id){
    array.splice(index,1)
  }
})
console.log(array)
豫ICP备17047052号-1