js 判断一个字符串是否在二维数组中,存在则删除此条数据

2022-06-21  本文已影响0人  上海_前端_求内推

判断字符串是否在二维数组中存在,存在则删除该元素
方法1

let arr = [{id:1,name:'lxn'},{id:2,name:'lm'},{id:3,name:'ydd'},{id:4,name:'zgj'}]
let id = 4;
var a = arr.filter(function (item) {
  if(id != item.id ){
      return item
  }
})
arr = a
console.log(arr)

方法2

let arr = [{id:1,name:'lxn'},{id:2,name:'lm'},{id:3,name:'ydd'},{id:4,name:'zgj'}]
let id = 2;
arr.forEach((item,index)=>{
  if(id == item.id){
    let a = arr.indexOf(item)
    arr.splice(a,1)
  }
})
console.log(arr)
上一篇下一篇

猜你喜欢

热点阅读