手写lodash库的differenceWith函数

61 阅读1分钟

手写loadsh函数的第二天

 <script>
      const arr = [
        { x: 1, y: 2 },
        { x: 2, y: 1 },
      ];
      function differenceWith(array, values, comparator) {
        return array.filter((item) =>
          values.every((val) => !comparator(item, val))
        );
      }

      console.log(
        differenceWith(
          arr,
          [{ x: 1, y: 2 }],
          (a, b) => a.x === b.x && a.y === b.y
        )
      );
    </script>