Friday, 16 June 2017

Sum of an array of numbers in Typescript

var shoppingListItems = [
    {name: "Milk", number: 100},
    {name: "Sugar", number: 22},
    {name: "Bread", number: 12}
];

// creates an array of the `number` property: [100, 22, 12]
var numbers = shoppingListItems.map(i => i.number);

// gets the sum of the array of numbers: 134
var sum = numbers.reduce((a, b) => a + b, 0);






1 comment:

  1. var sum = shoppingListItems.reduce((prev, curr) => prev + curr. number, 0);

    ReplyDelete