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

高洛峰
高洛峰Original
2017-02-06 09:46:421113browse

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

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!

Statement:
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