ts-data-forge
    Preparing search index...
    • Calculates the sum of values in an array by applying a mapping function to each element.

      Parameters

      • array: readonly []
      • mapFn: (item: unknown) => number

      Returns 0

      const numbers = [
      { value: 1 },
      { value: 2 },
      { value: 3 },
      { value: 4 },
      ] as const;

      const negatives = [{ value: 3 }, { value: -2 }, { value: 5 }] as const;

      const total = Arr.sumBy(numbers, (item) => item.value);

      const totalNegatives = Arr.sumBy(negatives, (item) => item.value);

      assert.isTrue(total === 10);

      assert.isTrue(totalNegatives === 6);
    • Calculates the sum of values in an array by applying a mapping function to each element.

      Type Parameters

      • N extends NonNegativeInt
      • E

      Parameters

      • array: readonly E[]
      • mapFn: (item: E) => N

      Returns NonNegativeInt

      const numbers = [
      { value: 1 },
      { value: 2 },
      { value: 3 },
      { value: 4 },
      ] as const;

      const negatives = [{ value: 3 }, { value: -2 }, { value: 5 }] as const;

      const total = Arr.sumBy(numbers, (item) => item.value);

      const totalNegatives = Arr.sumBy(negatives, (item) => item.value);

      assert.isTrue(total === 10);

      assert.isTrue(totalNegatives === 6);
    • Calculates the sum of values in an array by applying a mapping function to each element.

      Type Parameters

      • N extends Int
      • E

      Parameters

      • array: readonly E[]
      • mapFn: (item: E) => N

      Returns Int

      const numbers = [
      { value: 1 },
      { value: 2 },
      { value: 3 },
      { value: 4 },
      ] as const;

      const negatives = [{ value: 3 }, { value: -2 }, { value: 5 }] as const;

      const total = Arr.sumBy(numbers, (item) => item.value);

      const totalNegatives = Arr.sumBy(negatives, (item) => item.value);

      assert.isTrue(total === 10);

      assert.isTrue(totalNegatives === 6);
    • Calculates the sum of values in an array by applying a mapping function to each element.

      Type Parameters

      • N extends number
      • E

      Parameters

      • array: readonly E[]
      • mapFn: (item: E) => N

      Returns number

      const numbers = [
      { value: 1 },
      { value: 2 },
      { value: 3 },
      { value: 4 },
      ] as const;

      const negatives = [{ value: 3 }, { value: -2 }, { value: 5 }] as const;

      const total = Arr.sumBy(numbers, (item) => item.value);

      const totalNegatives = Arr.sumBy(negatives, (item) => item.value);

      assert.isTrue(total === 10);

      assert.isTrue(totalNegatives === 6);