_. Compact False value of the filter array
/* * @static * @memberof _ * @since 0.1.0 * @category Array * @param {Array} Array The Array to compact. * @returns {Array} Returns the new array of filtered values. * @example * * _.compact([0, 1, false, 2, '', 3]); */ / => [1, 2, 3] */ function compact(array) {var index = 1, array == null? ResIndex = 0, // array initial result = []; while (++index < length) { var value = array[index]; If (value) {result[resIndex++] = value; } } return result; }Copy the code
The overall idea:
- Loop through the array to determine the length of the array
- Determines if each item in the array has a value, and if so, pushes the new array
- Pushing out new arrays