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:
679 bytes
|
Rev | Line | |
---|
[d565449] | 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 | export default compact;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.