bf513906735c16c2c7cc0efd8ed21890ce95b476
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / remove.js
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 with
7  * three arguments: (value, index, array).
8  *
9  * **Note:** Unlike `_.filter`, this method mutates `array`.
10  *
11  * @static
12  * @memberOf _
13  * @category Array
14  * @param {Array} array The array to modify.
15  * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
16  * @returns {Array} Returns the new array of removed elements.
17  * @example
18  *
19  * var array = [1, 2, 3, 4];
20  * var evens = _.remove(array, function(n) {
21  *   return n % 2 == 0;
22  * });
23  *
24  * console.log(array);
25  * // => [1, 3]
26  *
27  * console.log(evens);
28  * // => [2, 4]
29  */
30 function remove(array, predicate) {
31   var result = [];
32   if (!(array && array.length)) {
33     return result;
34   }
35   var index = -1,
36       indexes = [],
37       length = array.length;
38
39   predicate = baseIteratee(predicate, 3);
40   while (++index < length) {
41     var value = array[index];
42     if (predicate(value, index, array)) {
43       result.push(value);
44       indexes.push(index);
45     }
46   }
47   basePullAt(array, indexes);
48   return result;
49 }
50
51 module.exports = remove;