js 数组插入和删除处理

function insertArray(arr, val, compare, maxLen) {
  //返回位置
  const index = arr.findIndex(compare)
  if (index === 0) {
    return
  }
  if (index > 0) {
      //删除一个
    arr.splice(index, 1)
  }
  //再插入(unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度)
  arr.unshift(val)
  if (maxLen && arr.length > maxLen) {
      //pop() 方法用于删除并返回数组的最后一个元素。
    arr.pop()
  }
}

 删除

function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare)
  if (index > -1) {
    arr.splice(index, 1)
  }
}
原文地址:https://www.cnblogs.com/Byme/p/9773512.html