You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compact.js 681B

12345678910111213141516171819202122232425262728293031
  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. while (++index < length) {
  22. var value = array[index];
  23. if (value) {
  24. result[resIndex++] = value;
  25. }
  26. }
  27. return result;
  28. }
  29. module.exports = compact;