The best way to remove array element by value

后端 未结 10 1325
北荒
北荒 2020-12-25 10:18

I have an array like this

arr = [\"orange\",\"red\",\"black\",\"white\"]

I want to augment the array object defining a deleteElem()<

相关标签:
10条回答
  • 2020-12-25 10:20

    The trick is to go through the array from end to beginning, so you don't mess up the indices while removing elements.

    var deleteMe = function( arr, me ){
       var i = arr.length;
       while( i-- ) if(arr[i] === me ) arr.splice(i,1);
    }
    
    var arr = ["orange","red","black", "orange", "white" , "orange" ];
    
    deleteMe( arr , "orange");
    

    arr is now ["red", "black", "white"]

    0 讨论(0)
  • 2020-12-25 10:23

    The best way is to use splice and rebuild new array, because after splice, the length of array does't change.

    Check out my answer:

    function remove_array_value(array, value) {
        var index = array.indexOf(value);
        if (index >= 0) {
            array.splice(index, 1);
            reindex_array(array);
        }
    }
    function reindex_array(array) {
       var result = [];
        for (var key in array) {
            result.push(array[key]);
        }
        return result;
    }
    

    example:

    var example_arr = ['apple', 'banana', 'lemon'];   // length = 3
    remove_array_value(example_arr, 'banana');
    

    banana is deleted and array length = 2

    0 讨论(0)
  • 2020-12-25 10:25

    Here you go:

    arr.deleteElem = function ( val ) {
        for ( var i = 0; i < this.length; i++ ) {
            if ( this[i] === val ) {
                this.splice( i, 1 );
                return i;
            }
        }
    };
    

    Live demo: http://jsfiddle.net/4vaE2/3/

    The deleteElem method returns the index of the removed element.

    var idx = arr.deleteElem( 'red' ); // idx is 1
    
    0 讨论(0)
  • 2020-12-25 10:27
    Array.prototype.deleteElem = function(val) {
        var index = this.indexOf(val); 
        if (index >= 0) this.splice(index, 1);
        return this;
    }; 
    var arr = ["orange","red","black","white"];
    var arr2 = arr.deleteElem("red");
    
    0 讨论(0)
  • 2020-12-25 10:29

    Or simply check all items, create a new array with non equal and return it.

    var arr = ['orange', 'red', 'black', 'white'];
    
    console.info('before: ' + JSON.stringify(arr));
    
    var deleteElem = function ( val ) {
        var new_arr = [];
        for ( var i = 0; i < this.length; i++ ) {
            if ( this[i] !== val ) {
                new_arr.push(this[i]);
            }
        }
        return new_arr;
    };
    
    arr = deleteElem('red');
    
    console.info('after: ' + JSON.stringify(arr));
    

    http://jsfiddle.net/jthavn3m/

    0 讨论(0)
  • 2020-12-25 10:30

    My approach, let's see what others have to say. It supports an "equals" method as well.

     // Remove array value
     // @param {Object} val
     Array.prototype.removeByValue = function (val) {
        for (var i = 0; i < this.length; i++) {
           var c = this[i];
           if (c == val || (val.equals && val.equals(c))) {
              this.splice(i, 1);
              break;
           }
        }
     };
    

    Read https://stackoverflow.com/a/3010848/356726 for the impact on iterations when using prototype with Array.

    0 讨论(0)
提交回复
热议问题