UNPKG

@xmini/qs

Version:

qs

39 lines (34 loc) 976 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compact = compact; /** * Creates an array with all falsey values removed. The values `false`, `null`, * `0`, `""`, `undefined`, and `NaN` are falsey. * * @since 0.1.0 * @category Array * @param {Array} array The array to compact. * @param {Array} invalid 过滤无效值,默认 ['', 0, undefined, false, NaN] * @returns {Array} Returns the new array of filtered values. * @example * * compact([0, 1, false, 2, '', 3]) * // => [1, 2, 3] */ function compact(array, invalid = ['', 0, undefined, false, NaN]) { let resIndex = 0; const result = []; /* eslint eqeqeq: 0 */ if (array == null) { return result; } for (const value of array) { if (!invalid.includes(value)) { result[resIndex++] = value; } } return result; } // const test1 = [1, 'test', '', 0, undefined, false, null, NaN]; // console.log(compact(test1, [0, undefined, false]));