Count the sum of values ​​of a specific field in an array of objects

This entry was posted in Typescript on 2022-05-12 by Eddy
708

Let's assume we have an array of object

const array = [
{
     id: 1,
     name: 'My first object',
     value: 5
},
{
     id: 1,
     name: 'My second object',
     value: 10
},
{
     id: 1,
     name: 'My third object',
     value: 20
}
]

We want to calculate the sum of the field 'value'

const sum = array.map((item) => {
return item.value;
}).reduce((sum, current) => sum + current, 0);
console.log(sum)

will return 35

This syntax is much classier than a for loop, isn't it? ;)

Comments

Affichage de 1-10 sur 17 éléments.

Williamzob e*m*i*.*.*.*.*6*8@gmail.com

порно зрелые ре...

Jimmywoody a*a*i*w*e*1*8*8*@rambler.ru


химия купить...

JefferyNaw s*.*r*o*z*.*t*c@gmail.com

бесплатное порн...

Robertnub v*w*l*b*l*c*m*9*4*@rambler.ru

click here to i...

StevenHAx s*g*d*h*2*@gmail.com

lead-market.ru...

KeithTuh w*n*o*u*h*a*9*6*4@rambler.ru

ладный сайт куп...

Tommypam c*m*n*a*e*9*8*4@rambler.ru

заказать ддос а...

MichaelPhove l*n*d*n*j*m*h*1*7*9*@rambler.ru

безукоризненный...

JamesCaf s*g*7*9*7*9*@gmail.com

порно эротика а...

RichardApell i*.*.*.*.*a*r*i*e*v*a@gmail.com

порно большие в...

Write comments

Math, for example, 45-12 = 33

Subscribe Now

You will receive a notification to each new article.

Tag Cloud

  • Composer
  • Yii2
  • OpenGraph
  • Helper
  • Wordpress
  • PhpMyAdmin
  • .htaccess
  • Config
  • Production
  • Git
  • Api
  • Mailchimp
  • Github
  • Vue.js