Remove multiple elements from array in Javascript/jQuery

后端 未结 22 2369
梦毁少年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条回答
  •  Happy的楠姐
    2021-01-29 20:46

    filter + indexOf (IE9+):

    function removeMany(array, indexes) {
      return array.filter(function(_, idx) {
        return indexes.indexOf(idx) === -1;
      });
    }); 
    

    Or with ES6 filter + find (Edge+):

    function removeMany(array, indexes = []) {
      return array.filter((_, idx) => indexes.indexOf(idx) === -1)
    }
    

提交回复
热议问题