Simple implementation of add delete check for javascript js operation array
- 2021-06-29 06:19:46
- OfStack
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);
};
Use:
myarray.remove (i); //delete
//Update
myarray.remove(0);
myarray.insert(0,eventjson[0]);
//Insert
myarray.insert(0,eventjson[0]);