Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseCallback = require('../internal/baseCallback'),
2
basePullAt = require('../internal/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 bound to
7
* `thisArg` and invoked with three arguments: (value, index, array).
8
*
9
* If a property name is provided for `predicate` the created `_.property`
10
* style callback returns the property value of the given element.
11
*
12
* If a value is also provided for `thisArg` the created `_.matchesProperty`
13
* style callback returns `true` for elements that have a matching property
14
* value, else `false`.
15
*
16
* If an object is provided for `predicate` the created `_.matches` style
17
* callback returns `true` for elements that have the properties of the given
18
* object, else `false`.
19
*
20
* **Note:** Unlike `_.filter`, this method mutates `array`.
21
*
22
* @static
23
* @memberOf _
24
* @category Array
25
* @param {Array} array The array to modify.
26
* @param {Function|Object|string} [predicate=_.identity] The function invoked
27
* per iteration.
28
* @param {*} [thisArg] The `this` binding of `predicate`.
29
* @returns {Array} Returns the new array of removed elements.
30
* @example
31
*
32
* var array = [1, 2, 3, 4];
33
* var evens = _.remove(array, function(n) {
34
* return n % 2 == 0;
35
* });
36
*
37
* console.log(array);
38
* // => [1, 3]
39
*
40
* console.log(evens);
41
* // => [2, 4]
42
*/
43
function remove(array, predicate, thisArg) {
44
var result = [];
45
if (!(array && array.length)) {
46
return result;
47
}
48
var index = -1,
49
indexes = [],
50
length = array.length;
51
52
predicate = baseCallback(predicate, thisArg, 3);
53
while (++index < length) {
54
var value = array[index];
55
if (predicate(value, index, array)) {
56
result.push(value);
57
indexes.push(index);
58
}
59
}
60
basePullAt(array, indexes);
61
return result;
62
}
63
64
module.exports = remove;
65
66