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

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

云罗郡主
Release: 2018-10-11 17:08:02
forward
2159 people have browsed it

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()
  }
}
Copy after login

Delete

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

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template