Remove Object from Array using JavaScript

前端 未结 29 2553
南笙
南笙 2020-11-22 10:24

How can I remove an object from an array? I wish to remove the object that includes name Kristian from someArray. For example:

som         


        
29条回答
  •  攒了一身酷
    2020-11-22 10:56

    This is what I use.

    Array.prototype.delete = function(pos){
        this[pos] = undefined;
        var len = this.length - 1;
        for(var a = pos;a < this.length - 1;a++){
          this[a] = this[a+1];
        }
        this.pop();
      }
    

    Then it is as simple as saying

    var myArray = [1,2,3,4,5,6,7,8,9];
    myArray.delete(3);
    

    Replace any number in place of three. After the expected output should be:

    console.log(myArray); //Expected output 1,2,3,5,6,7,8,9
    

提交回复
热议问题