javascript - remove array element on condition

后端 未结 7 820
北荒
北荒 2020-11-29 05:39

I was wondering how I\'d go about implementing a method in javascript that removes all elements of an array that clear a certain condition. (Preferably without using jQuery)

7条回答
  •  余生分开走
    2020-11-29 06:16

    You can use lodash.remove

    var array = [1, 2, 3, 4];
    var evens = _.remove(array, function(n) {
      return n % 2 == 0;
    });
    
    console.log(array);
    // => [1, 3]
    
    console.log(evens);
    // => [2, 4]
    

提交回复
热议问题