Deleting array elements in JavaScript - delete vs splice

后端 未结 27 3572
予麋鹿
予麋鹿 2020-11-21 05:31

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method?

For example:

myArray = [\         


        
相关标签:
27条回答
  • 2020-11-21 06:05

    Easiest way is probably

    var myArray = ['a', 'b', 'c', 'd'];
    delete myArray[1]; // ['a', undefined, 'c', 'd']. Then use lodash compact method to remove false, null, 0, "", undefined and NaN
    myArray = _.compact(myArray); ['a', 'c', 'd'];
    

    Hope this helps. Reference: https://lodash.com/docs#compact

    0 讨论(0)
  • 2020-11-21 06:08

    IndexOf accepts also a reference type. Suppose the following scenario:

    var arr = [{item: 1}, {item: 2}, {item: 3}];
    var found = find(2, 3); //pseudo code: will return [{item: 2}, {item:3}]
    var l = found.length;
    
    while(l--) {
       var index = arr.indexOf(found[l])
          arr.splice(index, 1);
       }
       
    console.log(arr.length); //1

    Differently:

    var item2 = findUnique(2); //will return {item: 2}
    var l = arr.length;
    var found = false;
      while(!found && l--) {
      found = arr[l] === item2;
    }
    
    console.log(l, arr[l]);// l is index, arr[l] is the item you look for
    
    0 讨论(0)
  • 2020-11-21 06:10

    Because delete only removes the object from the element in the array, the length of the array won't change. Splice removes the object and shortens the array.

    The following code will display "a", "b", "undefined", "d"

    myArray = ['a', 'b', 'c', 'd']; delete myArray[2];
    
    for (var count = 0; count < myArray.length; count++) {
        alert(myArray[count]);
    }
    

    Whereas this will display "a", "b", "d"

    myArray = ['a', 'b', 'c', 'd']; myArray.splice(2,1);
    
    for (var count = 0; count < myArray.length; count++) {
        alert(myArray[count]);
    }
    
    0 讨论(0)
  • 2020-11-21 06:10

    It's probably also worth mentioning that splice only works on arrays. (Object properties can't be relied on to follow a consistent order.)

    To remove the key-value pair from an object, delete is actually what you want:

    delete myObj.propName;     // , or:
    delete myObj["propName"];  // Equivalent.
    
    0 讨论(0)
  • 2020-11-21 06:10

    delete acts like a non real world situation, it just removes the item, but the array length stays the same:

    example from node terminal:

    > var arr = ["a","b","c","d"];
    > delete arr[2]
    true
    > arr
    [ 'a', 'b', , 'd', 'e' ]
    

    Here is a function to remove an item of an array by index, using slice(), it takes the arr as the first arg, and the index of the member you want to delete as the second argument. As you can see, it actually deletes the member of the array, and will reduce the array length by 1

    function(arr,arrIndex){
        return arr.slice(0,arrIndex).concat(arr.slice(arrIndex + 1));
    }
    

    What the function above does is take all the members up to the index, and all the members after the index , and concatenates them together, and returns the result.

    Here is an example using the function above as a node module, seeing the terminal will be useful:

    > var arr = ["a","b","c","d"]
    > arr
    [ 'a', 'b', 'c', 'd' ]
    > arr.length
    4 
    > var arrayRemoveIndex = require("./lib/array_remove_index");
    > var newArray = arrayRemoveIndex(arr,arr.indexOf('c'))
    > newArray
    [ 'a', 'b', 'd' ] // c ya later
    > newArray.length
    3
    

    please note that this will not work one array with dupes in it, because indexOf("c") will just get the first occurance, and only splice out and remove the first "c" it finds.

    0 讨论(0)
  • 2020-11-21 06:11

    I stumbled onto this question while trying to understand how to remove every occurrence of an element from an Array. Here's a comparison of splice and delete for removing every 'c' from the items Array.

    var items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];
    
    while (items.indexOf('c') !== -1) {
      items.splice(items.indexOf('c'), 1);
    }
    
    console.log(items); // ["a", "b", "d", "a", "b", "d"]
    
    items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];
    
    while (items.indexOf('c') !== -1) {
      delete items[items.indexOf('c')];
    }
    
    console.log(items); // ["a", "b", undefined, "d", "a", "b", undefined, "d"]
    ​
    
    0 讨论(0)
提交回复
热议问题