Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Accumulate value by types in objects

I have an array containing objects for each object has a different type (wall type). I am trying to accumulate the total value for each area in order to check the total area for each wall:

const walls = [
  {
    typeName: "Sandwichelement - 480mm",
    area: 28.165,
  },
  {
    typeName: "Sandwichelement - 480mm",
    area: 22.165,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 89.15,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 165,
  },
  {
    typeName: "Fundament - 900mm",
    area: 45.2,
  },
  {
    typeName: "Fundament - 900mm",
    area: 16.5,
  },
];

const result = walls.reduce((acc, val) => {
  acc[val.typeName] = { totalArea: 0 };

  acc[val.typeName]["totalArea"] += val.area;

//the result:
//{
//"Beton vægelement - 150mm": {totalArea: 254.15},
//"Fundament - 900mm": {totalArea: 61.7},
//"Sandwichelement - 480mm": {totalArea: 50.33}
//}

  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Iterate through it and update a result object:

const result = {};

for (const {typeName, area} of walls) {
    if (!result[typeName]) {
        result[typeName] = {totalArea: 0};
    }

    result[typeName].totalArea += area;
}

console.log(result);
/*
{
"Beton vægelement - 150mm": {totalArea: 254.15},
"Fundament - 900mm": {totalArea: 61.7},
"Sandwichelement - 480mm": {totalArea: 50.33}
}
*/

const walls = [
  {
    typeName: "Sandwichelement - 480mm",
    area: 28.165,
  },
  {
    typeName: "Sandwichelement - 480mm",
    area: 22.165,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 89.15,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 165,
  },
  {
    typeName: "Fundament - 900mm",
    area: 45.2,
  },
  {
    typeName: "Fundament - 900mm",
    area: 16.5,
  },
];

const result = {};

for (const {typeName, area} of walls) {
    if (!result[typeName]) {
        result[typeName] = {totalArea: 0};
    }

    result[typeName].totalArea += area;
}

console.log(result);
/*
{
"Beton vægelement - 150mm": {totalArea: 254.15},
"Fundament - 900mm": {totalArea: 61.7},
"Sandwichelement - 480mm": {totalArea: 50.33}
}
*/

about 4 years ago · Juan Pablo Isaza Report

0

You have only to iterate your walls array. In my case i use a foreach loop and add the objects with the totals to it.

const walls = [
  {
    typeName: "Sandwichelement - 480mm",
    area: 28.165,
  },
  {
    typeName: "Sandwichelement - 480mm",
    area: 22.165,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 89.15,
  },
  {
    typeName: "Beton vægelement - 150mm",
    area: 165,
  },
  {
    typeName: "Fundament - 900mm",
    area: 45.2,
  },
  {
    typeName: "Fundament - 900mm",
    area: 16.5,
  },
];

const s = {};
walls.forEach((val) => {
  if (! s[val.typeName]){
    s[val.typeName] = { totalArea: 0 };
    s[val.typeName]["totalArea"] += val.area;     } else {
      s[val.typeName]["totalArea"] += val.area;
  }
    
});

console.log("sum", s)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!