54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import baseIteratee from './_baseIteratee.js';
 | |
| import basePullAt from './_basePullAt.js';
 | |
| 
 | |
| /**
 | |
|  * Removes all elements from `array` that `predicate` returns truthy for
 | |
|  * and returns an array of the removed elements. The predicate is invoked
 | |
|  * with three arguments: (value, index, array).
 | |
|  *
 | |
|  * **Note:** Unlike `_.filter`, this method mutates `array`. Use `_.pull`
 | |
|  * to pull elements from an array by value.
 | |
|  *
 | |
|  * @static
 | |
|  * @memberOf _
 | |
|  * @since 2.0.0
 | |
|  * @category Array
 | |
|  * @param {Array} array The array to modify.
 | |
|  * @param {Function} [predicate=_.identity] The function invoked per iteration.
 | |
|  * @returns {Array} Returns the new array of removed elements.
 | |
|  * @example
 | |
|  *
 | |
|  * 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]
 | |
|  */
 | |
| function remove(array, predicate) {
 | |
|   var result = [];
 | |
|   if (!(array && array.length)) {
 | |
|     return result;
 | |
|   }
 | |
|   var index = -1,
 | |
|       indexes = [],
 | |
|       length = array.length;
 | |
| 
 | |
|   predicate = baseIteratee(predicate, 3);
 | |
|   while (++index < length) {
 | |
|     var value = array[index];
 | |
|     if (predicate(value, index, array)) {
 | |
|       result.push(value);
 | |
|       indexes.push(index);
 | |
|     }
 | |
|   }
 | |
|   basePullAt(array, indexes);
 | |
|   return result;
 | |
| }
 | |
| 
 | |
| export default remove;
 |