Home > Web Front-end > JS Tutorial > Simple implementation of javascript js operation array addition, deletion, modification and search

Simple implementation of javascript js operation array addition, deletion, modification and search

高洛峰
Release: 2017-02-06 09:46:42
Original
1111 people have browsed it

Function definition

Array.prototype.indexOf = function(val) {
 
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
  }
  return -1;
};
Array.prototype.remove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};
Array.prototype.insert = function (index, item) {
 this.splice(index, 0, item);
 
};
Copy after login

Usage:

myarray.remove(i); //Delete

//Update

myarray.remove(0);
myarray.insert(0,eventjson[0]);

//Insert

myarray.insert(0,eventjson[0]);

The above simple implementation of adding, deleting, modifying and checking the javascript js operation array is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more javascript js operation array simple implementation of addition, deletion, modification and search related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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