Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested reduce function not working for my array of array objects

I am building a store which can receive orders which I have simulated here with the order arrays. Those orders can contain several different items which I am representing with the name property in the item objects inside the order arrays. And those items can have any quantity aswell.

What I need to to calculate the total amount of units that have been sold of a certain item type. This means I need to loop through all the items in all the orders, and if the item is the type I am looking for, then I need to add that items quantity to the total amount of units for that type.

I made this nested reduce function, but for some reason it is giving the wrong awnsers. What am I doing worng?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const order_1 = [
  { name: "1", quantity: 1 },
  { name: "2", quantity: 3 },
];
const order_2 = [
  { name: "3", quantity: 1 },
  { name: "1", quantity: 2 },
];
const order_3 = [
  { name: "4", quantity: 1 },
  { name: "2", quantity: 1 },
];
const order_4 = [
  { name: "2", quantity: 2 },
  { name: "1", quantity: 1 },
];
const orders = [order_1, order_2, order_3, order_4];

function getTotalQuantity(type) {
  return orders.reduce((prevOrder, currOrder) => {
    return prevOrder + currOrder.reduce((prevItem, currItem) => {
      return prevItem + (currItem.name === type) ? currItem.quantity : 0;
    }, 0);
  }, 0);
}
console.log("1: " + getTotalQuantity("1"));
console.log("2: " + getTotalQuantity("2"));
console.log("3: " + getTotalQuantity("3"));
console.log("4: " + getTotalQuantity("4"));

>Solution :

Two things:

  1. You never use type
  2. Your parenthesis are incorrect

Should be:

(currItem.name === type ? currItem.quantity : 0)

And not:

(currItem.name === type) ? currItem.quantity : 0
const order_1 = [
  { name: "1", quantity: 1 },
  { name: "2", quantity: 3 },
];
const order_2 = [
  { name: "3", quantity: 1 },
  { name: "1", quantity: 2 },
];
const order_3 = [
  { name: "4", quantity: 1 },
  { name: "2", quantity: 1 },
];
const order_4 = [
  { name: "2", quantity: 2 },
  { name: "1", quantity: 1 },
];
const orders = [order_1, order_2, order_3, order_4];

function getTotalQuantity(type) {
  return orders.reduce((prevOrder, currOrder) => {
    return prevOrder + currOrder.reduce((prevItem, currItem) => {
      return prevItem + (currItem.name === type ? currItem.quantity : 0);
    }, 0);
  }, 0);
}
console.log("1: " + getTotalQuantity("1"));
console.log("2: " + getTotalQuantity("2"));
console.log("3: " + getTotalQuantity("3"));
console.log("4: " + getTotalQuantity("4"));

Also, this new code works fine, but I would use this (less efficient, but more readable):

const order_1 = [
  { name: "1", quantity: 1 },
  { name: "2", quantity: 3 },
];
const order_2 = [
  { name: "3", quantity: 1 },
  { name: "1", quantity: 2 },
];
const order_3 = [
  { name: "4", quantity: 1 },
  { name: "2", quantity: 1 },
];
const order_4 = [
  { name: "2", quantity: 2 },
  { name: "1", quantity: 1 },
];
const orders = [order_1, order_2, order_3, order_4];

function getTotalQuantity(type) {
  return orders.flat().filter(item => item.name === type).reduce((prevItem, currItem) => {
    return prevItem + currItem.quantity;
  }, 0);
}
console.log("1: " + getTotalQuantity("1"));
console.log("2: " + getTotalQuantity("2"));
console.log("3: " + getTotalQuantity("3"));
console.log("4: " + getTotalQuantity("4"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading