Home > Mobile >  Nested reduce function not working for my array of array objects
Nested reduce function not working for my array of array objects

Time:08-27

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?

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"));

CodePudding user response:

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"));

CodePudding user response:

Fixing with currItem.name === type ? prevItem currItem.quantity : prevItem; is working fine now

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 currItem.name === type ? prevItem   currItem.quantity : prevItem;
      }, 0)
    );
  }, 0);
}

console.log("1: "   getTotalQuantity("1"));
console.log("2: "   getTotalQuantity("2"));
console.log("3: "   getTotalQuantity("3"));
console.log("4: "   getTotalQuantity("4"));

Another version with simple forEach

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, sum = 0) {
  orders.forEach((arr) =>
    arr.forEach(({ name, quantity }) => {
      if (name === type) {
        sum  = quantity;
      }
    })
  );
  return sum;
}

console.log("1: "   getTotalQuantity("1"));
console.log("2: "   getTotalQuantity("2"));
console.log("3: "   getTotalQuantity("3"));
console.log("4: "   getTotalQuantity("4"));

  • Related