Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

summing values from a list typescript

export interface data {
  id: number;
  cost: number;
}

array: data[] = [
  { id: 1, cost: 25 },
  { id: 2, cost: 25 },
  { id: 3, cost: 50 },
  { id: 4, cost: 200 },
  { id: 5, cost: 10 },
  { id: 6, cost: 60 },
];

result: number = arr.reduce((accumulator, obj) => {
  return accumulator + obj.cost;
}, 0);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #summing #values #list #typescript
ADD COMMENT
Topic
Name
1+6 =