Remove multiple elements from array in Javascript/jQuery

后端 未结 22 2107
梦毁少年i
梦毁少年i 2021-01-29 19:42

I have two arrays. The first array contains some values while the second array contains indices of the values which should be removed from the first array. For example:

<
相关标签:
22条回答
  • 2021-01-29 20:29

    You could construct a Set from the array and then create an array from the set.

    const array = [1, 1, 2, 3, 5, 5, 1];
    const uniqueArray = [...new Set(array)];
    console.log(uniqueArray); // Result: [1, 2, 3, 5]
    
    0 讨论(0)
  • 2021-01-29 20:31

    You can correct your code by replacing removeValFromIndex with removeValFromIndex.reverse(). If that array is not guaranteed to use ascending order, you can instead use removeValFromIndex.sort(function(a, b) { return b - a }).

    0 讨论(0)
  • 2021-01-29 20:32

    This works well for me and work when deleting from an array of objects too:

    var array = [ 
        { id: 1, name: 'bob', faveColor: 'blue' }, 
        { id: 2, name: 'jane', faveColor: 'red' }, 
        { id: 3, name: 'sam', faveColor: 'blue' }
    ];
    
    // remove people that like blue
    
    array.filter(x => x.faveColor === 'blue').forEach(x => array.splice(array.indexOf(x), 1));
    

    There might be a shorter more effecient way to write this but this does work.

    0 讨论(0)
  • 2021-01-29 20:33

    A simple solution using ES5. This seems more appropriate for most applications nowadays, since many do no longer want to rely on jQuery etc.

    When the indexes to be removed are sorted in ascending order:

    var valuesArr = ["v1", "v2", "v3", "v4", "v5"];   
    var removeValFromIndex = [0, 2, 4]; // ascending
    
    removeValFromIndex.reverse().forEach(function(index) {
      valuesArr.splice(index, 1);
    });
    

    When the indexes to be removed are not sorted:

    var valuesArr = ["v1", "v2", "v3", "v4", "v5"];   
    var removeValFromIndex = [2, 4, 0];  // unsorted
    
    removeValFromIndex.sort(function(a, b) { return b - a; }).forEach(function(index) {
      valuesArr.splice(index, 1);
    });
    
    0 讨论(0)
  • 2021-01-29 20:33

    For Multiple items or unique item:

    I suggest you use Array.prototype.filter

    Don't ever use indexOf if you already know the index!:

    var valuesArr = ["v1","v2","v3","v4","v5"];
    var removeValFrom = [0, 2, 4];
    
    valuesArr = valuesArr.filter(function(value, index) {
         return removeValFrom.indexOf(index) == -1;
    }); // BIG O(N*m) where N is length of valuesArr and m is length removeValFrom
    

    Do:

    with Hashes... using Array.prototype.map

      var valuesArr = ["v1","v2","v3","v4","v5"];
      var removeValFrom = {};
      ([0, 2, 4]).map(x=>removeValFrom[x]=1); //bild the hash.
      valuesArr = valuesArr.filter(function(value, index) {
          return removeValFrom[index] == 1;
      }); // BIG O(N) where N is valuesArr;
    
    0 讨论(0)
  • 2021-01-29 20:34
    function filtermethod(element, index, array) {  
        return removeValFromIndex.find(index)
    }  
    var result = valuesArr.filter(filtermethod);
    

    MDN reference is here

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