Delete the method of making element prototype in array

New people learn from and organize, if there are any deficiencies, please correct them

First, you can define a function for the array object of js to find the position of the specified element in the array, that is, the index. The code is:

<!--Note: this method is only applicable to arrays with pure strings, not for mixed arrays with strings and objects-->
Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
<!--There is another way to get the index value of the corresponding string in the array of pure strings-->
arr1.indexOf(str) <!--Return string str Index value--> 
arr1.findIndex(function(data) {
  return data == str;
});<!--Also return index value-->

<!--For a mixed array, the method to get the index value of the object is as follows:-->
var getArrIndex = function(arr, obj) {
    var index = null;
    var key = Object.keys(obj)[0];
    arr.every(function(value, i) {
        if (value[key] === obj[key]) {
            index = i;
            return false;
        }
        return true;
    });
    return index;
};

Then use the index to get the element, and use the intrinsic function of js array to delete the element: the code is:

<!--The first method-->
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
/*
* The second method
* Method: Array.baoremove(dx) 
* Function: delete array element 
* Parameter: dx deletes the subscript of the element 
* Return: modify the array on the original array 
*/
Array.prototype.baoremove = function(dx){ 
  if(isNaN(dx)||dx>this.length){return false;} 
  this.splice(dx,1); 
}
/* 
* The third method
* Method: Array.remove(dx) through traversal, reconstruct array 
* Function: delete array element 
* Parameter: dx deletes the subscript of the element 
*/
Array.prototype.remove=function(dx){ 
  if(isNaN(dx)||dx>this.length){return false;} 
  for(var i=0,n=0;i<this.length;i++) 
  { 
    if(this[i]!=this[dx]) 
    { 
      this[n++]=this[i] 
    } 
  } 
  this.length-=1 
}

In this way, a function is constructed. For example, I have an array:

    var emp = ['abs','dsf','sdf','fd']
    var arr2 = [{id:1,name:'lanyan'},
                {id:2,name:'wanming'},
                {id:3,name:'tingting'},
                {id:4,name:'songsong'},
                'test',
                'ethos'];

If we want to delete 'fd', we can use:

    <!--The first method-->
    emp.remove('fd');
    <!--The second method-->
    emp.remove(3)
    <!--The third method-->
    var num = getArrIndex(arr2,{id:2,name:'wanming'}) <!--Return to 1-->
    arr2.remove(num)

In the object array, get the subscript of an object in the array according to its property value.

Array.map(item => item.id).indexOf(mealid);
<!--id Is the property name of the lookup, mealid Is the property value by which to find-->

Keywords: Javascript

Added by someguyhere on Wed, 01 Jan 2020 07:08:29 +0200