function _compact(array) { var index = -1, resIndex = 0, length = array == null ? Zero: array.length, result = [] while (++index < length) { var value = array[index] if (value) { result[resIndex++] = array[index] } } return result } console.log(_compact([0, 1, false, 2, '', 3])); / / [1, 2, 3]Copy the code
Idea: reassemble a new array return using JS implicit conversion types