In the array

Use for, indexOf, splice

let str = ['a'.'b'.'c']
let index = str.indexOf('a')
if (index > -1) {
  // If the value is greater than 0,
  str.splice(index, 1) // Delete it if it exists
}
console.log(str) // ["b", "c"]
Copy the code

In an array object

The first way

Map, Filter, and includes are used

let arrData = [
  { id: 1.name: 'aa' },
  { id: 2.name: 'bb' },
  { id: 3.name: 'cc' },
  { id: 4.name: 'dd' },
  { id: 5.name: 'ee'}]let serviceList = [
  { id: 2.name: 'bb' },
  { id: 3.name: 'cc' },
  { id: 5.name: 'ee'}]let serviceIdList = serviceList.map((item) = > item.id)
console.log(serviceIdList)
let resultArr = arrData.filter((item) = >! serviceIdList.includes(item.id))console.log(resultArr)
Copy the code

The second way

Use for, indexOf, splice

let filterArr = [
   { id: '1'.name: 'aa' },
   { id: '2'.name: 'bb' },
   { id: '3'.name: 'cc' },
   { id: '4'.name: 'dd' },
   { id: '5'.name: 'ee'}]for (let i = 0; i < filterArr.length; i++) {
   if (filterArr[i].id.indexOf('4') > -1) {
     // Check whether the object with id 4 exists,
     filterArr.splice(i, 1) // Delete}}console.log(filterArr)
Copy the code