Deleting array elements in JavaScript - delete vs splice

后端 未结 27 3573
予麋鹿
予麋鹿 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:22

    Why not just filter? I think it is the most clear way to consider the arrays in js.

    myArray = myArray.filter(function(item){
        return item.anProperty != whoShouldBeDeleted
    });
    
    0 讨论(0)
  • 2020-11-21 06:22

    If you have small array you can use filter:

    myArray = ['a', 'b', 'c', 'd'];
    myArray = myArray.filter(x => x !== 'b');
    
    0 讨论(0)
  • 2020-11-21 06:22
    function deleteFromArray(array, indexToDelete){
      var remain = new Array();
      for(var i in array){
        if(array[i] == indexToDelete){
          continue;
        }
        remain.push(array[i]);
      }
      return remain;
    }
    
    myArray = ['a', 'b', 'c', 'd'];
    deleteFromArray(myArray , 0);
    

    // result : myArray = ['b', 'c', 'd'];

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

    delete Vs splice

    when you delete an item from an array

    var arr = [1,2,3,4]; delete arr[2]; //result [1, 2, 3:, 4]
    console.log(arr)

    when you splice

    var arr = [1,2,3,4]; arr.splice(1,1); //result [1, 3, 4]
    console.log(arr);

    in case of delete the element is deleted but the index remains empty

    while in case of splice element is deleted and the index of rest elements is reduced accordingly

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

    From Core JavaScript 1.5 Reference > Operators > Special Operators > delete Operator :

    When you delete an array element, the array length is not affected. For example, if you delete a[3], a[4] is still a[4] and a[3] is undefined. This holds even if you delete the last element of the array (delete a[a.length-1]).

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

    If you want to iterate a large array and selectively delete elements, it would be expensive to call splice() for every delete because splice() would have to re-index subsequent elements every time. Because arrays are associative in Javascript, it would be more efficient to delete the individual elements then re-index the array afterwards.

    You can do it by building a new array. e.g

    function reindexArray( array )
    {
           var result = [];
            for( var key in array )
                    result.push( array[key] );
            return result;
    };
    

    But I don't think you can modify the key values in the original array, which would be more efficient - it looks like you might have to create a new array.

    Note that you don't need to check for the "undefined" entries as they don't actually exist and the for loop doesn't return them. It's an artifact of the array printing that displays them as undefined. They don't appear to exist in memory.

    It would be nice if you could use something like slice() which would be quicker, but it does not re-index. Anyone know of a better way?


    Actually, you can probably do it in place as follows which is probably more efficient, performance-wise:

    reindexArray : function( array )
    {
        var index = 0;                          // The index where the element should be
        for( var key in array )                 // Iterate the array
        {
            if( parseInt( key ) !== index )     // If the element is out of sequence
            {
                array[index] = array[key];      // Move it to the correct, earlier position in the array
                ++index;                        // Update the index
            }
        }
    
        array.splice( index );  // Remove any remaining elements (These will be duplicates of earlier items)
    },
    
    0 讨论(0)
提交回复
热议问题