main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[d565449] | 1 | import baseIteratee from './_baseIteratee.js';
|
---|
| 2 | import basePullAt from './_basePullAt.js';
|
---|
| 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 | export default remove;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.