1 | var baseIteratee = require('./_baseIteratee'),
|
---|
2 | basePullAt = require('./_basePullAt');
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Removes all elements from `array` that `predicate` returns truthy for
|
---|
6 | * and returns an array of the removed elements. The predicate is invoked
|
---|
7 | * with three arguments: (value, index, array).
|
---|
8 | *
|
---|
9 | * **Note:** Unlike `_.filter`, this method mutates `array`. Use `_.pull`
|
---|
10 | * to pull elements from an array by value.
|
---|
11 | *
|
---|
12 | * @static
|
---|
13 | * @memberOf _
|
---|
14 | * @since 2.0.0
|
---|
15 | * @category Array
|
---|
16 | * @param {Array} array The array to modify.
|
---|
17 | * @param {Function} [predicate=_.identity] The function invoked per iteration.
|
---|
18 | * @returns {Array} Returns the new array of removed elements.
|
---|
19 | * @example
|
---|
20 | *
|
---|
21 | * var array = [1, 2, 3, 4];
|
---|
22 | * var evens = _.remove(array, function(n) {
|
---|
23 | * return n % 2 == 0;
|
---|
24 | * });
|
---|
25 | *
|
---|
26 | * console.log(array);
|
---|
27 | * // => [1, 3]
|
---|
28 | *
|
---|
29 | * console.log(evens);
|
---|
30 | * // => [2, 4]
|
---|
31 | */
|
---|
32 | function remove(array, predicate) {
|
---|
33 | var result = [];
|
---|
34 | if (!(array && array.length)) {
|
---|
35 | return result;
|
---|
36 | }
|
---|
37 | var index = -1,
|
---|
38 | indexes = [],
|
---|
39 | length = array.length;
|
---|
40 |
|
---|
41 | predicate = baseIteratee(predicate, 3);
|
---|
42 | while (++index < length) {
|
---|
43 | var value = array[index];
|
---|
44 | if (predicate(value, index, array)) {
|
---|
45 | result.push(value);
|
---|
46 | indexes.push(index);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | basePullAt(array, indexes);
|
---|
50 | return result;
|
---|
51 | }
|
---|
52 |
|
---|
53 | module.exports = remove;
|
---|