Home  >  Article  >  Web Front-end  >  Interpretation of js array insertion and deletion processing methods (with code)

Interpretation of js array insertion and deletion processing methods (with code)

云罗郡主
云罗郡主forward
2018-10-11 17:07:522126browse

The content of this article is about the interpretation of js array insertion and deletion processing methods (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Interpretation of js array insertion and deletion processing methods (with code)

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()
  }
}

Delete

function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare)  if (index > -1) {
    arr.splice(index, 1)
  }
}

The above is a complete introduction to the interpretation of js array insertion and deletion processing methods (with code), if you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.

The above is the detailed content of Interpretation of js array insertion and deletion processing methods (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete