手写lodash库的compact函数

64 阅读1分钟

手写loadsh函数的第一天

<script>
      const arr = ["0", "1", 2, 3, "", undefined];
      function compact(array) {
        const newArr = [];
        for (const item of array) {
          if (item) {
            newArr.push(item);
          }
        }
        return newArr;
      }
      console.log(compact(arr));
    </script>