UNPKG

just-compact

Version:

returns a copy of an array with falsey values removed

24 lines (21 loc) 482 B
module.exports = compact; /* compact([1, null, 2, undefined, null, NaN, 3, 4, false, 5]); // [1, 2, 3, 4, 5] compact([1, 2, [], 4, {}]); // [1, 2, [], 4, {}] compact([]); // [] compact({}); // throws */ function compact(arr) { if (!Array.isArray(arr)) { throw new Error('expected an array'); } var result = []; var len = arr.length; for (var i = 0; i < len; i++) { var elem = arr[i]; if (elem) { result.push(elem); } } return result; }