Dev Helpers
← Back

Type an accumulator for group totals

Type an accumulator for group totals using TypeScript.

Open this example in the interactive library

Source code

const entries: readonly (readonly [string, number])[] = [
  ["api", 2],
  ["web", 4],
];
console.log(
  entries.reduce<Record<string, number>>(
    (out, [k, v]) => ({ ...out, [k]: v }),
    {},
  ),
);

Sample output

{ api: 2, web: 4 }