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

204
Views
How to sum values in object with nested arrays?

I have the following structure:

groupedData = {
   rice: [
     {
       name: 'white rice'
       quantity: 2
       category: 'rice'
     },
     {
      name: 'brown rice'
      quantity: 1
      category: 'rice'
    }
   ],
   water: [
     {
       name: 'sparkle water'
       quantity: 1
       category: 'water'
     },
     {
      name: 'still water'
      quantity: 1
      category: 'water'
    }
   ],
   tea: [{
       name: 'green tea'
       quantity: 1
       category: 'water'
   }],
 }

What I want to achieve is to have a summed quantity of each category, so as: rice: 3, water: 2, tea: 1

I tried the following:

  const sum = () => {
    let quantity = []
    for (var property in groupedData) {
      for (let i=0; i< groupedData[property].length; i++){
      quantity[property] = groupedData[property][i].quantity;
      }
    }
    return quantity;
}

but it doesn't return summed data but only the quantity of the first item [rice: 2, water: 1, tea: 1]

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

0

Your solution does not add the nodes. Just add the quantity of each node

const groupedData = {
  rice: [
    { name: 'white rice', quantity: 2, category: 'rice' },
    { name: 'brown rice', quantity: 1, category: 'rice' }
  ],
  water: [
    { name: 'sparkle water', quantity: 1, category: 'water' },
    { name: 'still water', quantity: 1, category: 'water' }
  ],
  tea: [
    { name: 'green tea', quantity: 1, category: 'water' }],
};

const sum = () => {
  let quantity = {}
  for (var property in groupedData) {
    for (let i = 0; i < groupedData[property].length; i++) {
      quantity[property] = quantity[property] ? quantity[property] : 0;
      quantity[property] += groupedData[property][i].quantity;
    }
  }
  return quantity;
};
console.log(sum());

You can also make use of Object.entries, Array.forEach and Array.reduce to achieve this.

const groupedData = {
  rice: [
    { name: 'white rice', quantity: 2, category: 'rice' },
    { name: 'brown rice', quantity: 1, category: 'rice' }
  ],
  water: [
    { name: 'sparkle water', quantity: 1, category: 'water' },
    { name: 'still water', quantity: 1, category: 'water' }
  ],
  tea: [
    { name: 'green tea', quantity: 1, category: 'water' }],
};
const sum = () => {
  let quantity = {};
  Object.entries(groupedData).forEach(([key, value]) => {
    quantity[key] = value.reduce((acc, { quantity }) => acc += quantity, 0)
  });
  return quantity;
};
console.log(sum());

about 4 years ago · Juan Pablo Isaza Report

0

This is based on your question.

const groupedData = {
  rice: [{
      name: 'white rice',
      quantity: 2,
      category: 'rice',
    },
    {
      name: 'brown rice',
      quantity: 1,
      category: 'rice',
    }
  ],
  water: [{
      name: 'sparkle water',
      quantity: 1,
      category: 'water',
    },
    {
      name: 'still water',
      quantity: 1,
      category: 'water',
    }
  ],
  tea: [{
    name: 'green tea',
    quantity: 1,
    category: 'water',
  }],
}

const sum = () => {
  let quantity = {}
  for (var property in groupedData) {
    for (let i = 0; i < groupedData[property].length; i++) {
      quantity = {
        ...quantity,
        [property]: quantity[property] ? quantity[property] + groupedData[property][i].quantity :  groupedData[property][i].quantity
      }
    }
  }
  return quantity;
}

console.log(sum());

And this is my answer with reduce

const groupedData = {
  rice: [{
      name: 'white rice',
      quantity: 2,
      category: 'rice',
    },
    {
      name: 'brown rice',
      quantity: 1,
      category: 'rice',
    }
  ],
  water: [{
      name: 'sparkle water',
      quantity: 1,
      category: 'water',
    },
    {
      name: 'still water',
      quantity: 1,
      category: 'water',
    }
  ],
  tea: [{
    name: 'green tea',
    quantity: 1,
    category: 'water',
  }],
}

const sum = () => {
  let res = {}
  for (var props in groupedData) {
    const item = groupedData[props].reduce((acc, cur) => {
      acc += cur.quantity;
      return acc;
    }, 0);
    res[props] = item;
  }
  return res;
}

console.log(sum());

about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this:

const result = Object.entries(groupedData).reduce((prev, [k, v]) => {
  const sums = {
    ...prev,
    [k]: v.reduce((p, c) => p + c.quantity, 0),
  }
  return sums
}, {})

Here is the full snippet:

const groupedData = {
  rice: [{
      name: 'white rice',
      quantity: 2,
      category: 'rice',
    },
    {
      name: 'brown rice',
      quantity: 1,
      category: 'rice',
    },
  ],
  water: [{
      name: 'sparkle water',
      quantity: 1,
      category: 'water',
    },
    {
      name: 'still water',
      quantity: 1,
      category: 'water',
    },
  ],
  tea: [{
    name: 'green tea',
    quantity: 1,
    category: 'water',
  }, ],
}

const result = Object.entries(groupedData).reduce((prev, [k, v]) => {
  const sums = {
    ...prev,
    [k]: v.reduce((p, c) => p + c.quantity, 0),
  }
  return sums
}, {})

console.log(result)

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!