UNPKG

679 BJavaScriptView Raw
1/**
2 * Creates an array with all falsey values removed. The values `false`, `null`,
3 * `0`, `""`, `undefined`, and `NaN` are falsey.
4 *
5 * @static
6 * @memberOf _
7 * @since 0.1.0
8 * @category Array
9 * @param {Array} array The array to compact.
10 * @returns {Array} Returns the new array of filtered values.
11 * @example
12 *
13 * _.compact([0, 1, false, 2, '', 3]);
14 * // => [1, 2, 3]
15 */
16function compact(array) {
17 var index = -1,
18 length = array == null ? 0 : array.length,
19 resIndex = 0,
20 result = [];
21
22 while (++index < length) {
23 var value = array[index];
24 if (value) {
25 result[resIndex++] = value;
26 }
27 }
28 return result;
29}
30
31export default compact;