API Reference

Returns the sum of the values of a specific shared key in an array of objects.

function sumOfKeyValue<T extends object, U extends keyof T>(
  arr: (T & { [K in U]: number })[],
  key: U
)

// Example:

const obj1 = { a: 1, b: 2, c: 3 }
const obj2 = { a: 2, b: 3, c: 4 }

sumOfKeyValue([obj1, obj2], "c") //=> 7