Last change
on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
681 bytes
|
Line | |
---|
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 | */
|
---|
16 | function 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 |
|
---|
31 | module.exports = compact;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.