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

68
Views
sum object value in javascript by name

I have a data that type is object and I want to sum the value1 + value 2 as totalValue per each name by using for

//this is my object  
{
111:{name:'a', value1: 2 , value2: 2},
222:{name:'b', value1: 3 , value2: 3},
333:{name:'c', value1: 4 , value2: 4},
 }
 
 // this is the result that I expected
 {
 {name:'a' , totalValue:4},
 {name:'b' , totalValue:6},
 {name:'c' , totalValue:8},
 }

   

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

0

  1. Transform the object into an array of key value pairs using Object.entries

  2. Then map over the entries and calculate the totalValue property.

  3. Finally transform the array back to an object using Object.fromEntries.

const obj = {
  111: { name: "a", value1: 2, value2: 2 },
  222: { name: "b", value1: 3, value2: 3 },
  333: { name: "c", value1: 4, value2: 4 },
};

const result = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k, { name: v.name, totalValue: v.value1 + v.value2 }])
);

console.log(result);

If there are multiple value properties that you wish to add, then refer to the solution below:

const obj = {
  111: { name: "a", value1: 2, value2: 2 },
  222: { name: "b", value1: 3, value2: 3 },
  333: { name: "c", value1: 4, value2: 4 },
};

const result = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [
    k,
    {
      name: v.name,
      totalValue: Object.keys(v)
        .filter((k) => k.includes("value"))
        .reduce((s, k) => s + v[k], 0),
    },
  ])
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this

const obj = {
  111: {
    name: 'a',
    value1: 2,
    value2: 2,
    value3: 4
  },
  222: {
    name: 'b',
    value1: 3,
    value2: 3
  },
  333: {
    name: 'c',
    value1: 4,
    value2: 4
  },
};
const result = [];
for (const key in obj) {
  const item = obj[key];
  let total = 0;
  const final = {};
  Object.keys(item).forEach(key => {
    if (key.startsWith('value')) {
      total += item[key];
    } else {
      final[key] = item[key];
    }
  })
  final.totalValue = total;
  result.push(final);
}
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Your expected result is not a valid object.

Here is some possible codes.

const obj = {
  111:{name:'a', value1: 2 , value2: 2},
  222:{name:'b', value1: 3 , value2: 3},
  333:{name:'c', value1: 4 , value2: 4},
};
const arrResult = Object.values(obj)
  .map(({ name, value1, value2 }) => ({ name, totalValue: value1 + value2 }));
const objResult = Object.entries(obj)
  .map(([key, { name, value1, value2 }]) => ({ key, value: { name, totalValue: value1 + value2 } }))
  .reduce((acc, cur) => { acc[cur.key] = cur.value; return acc; }, {});
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!