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

180
Views
How to calcuate item quantiy from array of object if the itemid is same and how to sum up those quantity whose date are same too

I have data in this format

const ChkdArr = [
  {
    date: "2022-05-25",
    itemId: 29,
    quantity: 4,
  },
  {
    date: "2022-05-26",
    itemId: 29,
    quantity: 2,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 7,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 4,
  },
  {
    date: "2022-05-27",
    itemId: 36,
    quantity: 2,
  },
  {
    date: "2022-05-28",
    itemId: 47,
    quantity: 1,
  },
];

i am getting trouble to get a new array with those data whose itemid is same and in those item if the date is same sum up their quantity.

I want the result to look like this i am applying map and filters but my data is repeating and having incorrect results

const DateSortArr = [{
    itemId: 29,
    date: [
      { date: "2022-05-25", quantity: 4 },
      { date: "2022-05-26", quantity: 2 },
    ],
},
  {
    itemId: 36,
    date: [
      { date: "2022-05-25", quantity: 11 },
      { date: "2022-05-27", quantity: 2 },
    ],},
{
    itemId: 47,
    date: [{ date: "2022-05-25", quantity: 1 }],
  },
];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Hello I don't know id undrestand you but you use this fun :

function groupBy(collection, property) {
  var i = 0, val, index,
      values = [], result = [];
  for (; i < collection.length; i++) {
      val = collection[i][property];
      index = values.indexOf(val);
      if (index > -1)
          result[index].push(collection[i]);
      else {
          values.push(val);
          result.push([collection[i]]);
      }
  }
  return result;
}

const ChkdArr = [
  {
    date: "2022-05-25",
    itemId: 29,
    quantity: 4,
  },
  {
    date: "2022-05-26",
    itemId: 29,
    quantity: 2,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 7,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 4,
  },
  {
    date: "2022-05-27",
    itemId: 36,
    quantity: 2,
  },
  {
    date: "2022-05-28",
    itemId: 47,
    quantity: 1,
  },
];
var obj = groupBy(ChkdArr, "date");

console.log(obj)
about 4 years ago · Juan Pablo Isaza Report

0

const ChkdArr = [
  {
    date: "2022-05-25",
    itemId: 29,
    quantity: 4,
  },
  {
    date: "2022-05-26",
    itemId: 29,
    quantity: 2,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 7,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 4,
  },
  {
    date: "2022-05-27",
    itemId: 36,
    quantity: 2,
  },
  {
    date: "2022-05-28",
    itemId: 47,
    quantity: 1,
  },
];

let answer = [];

ChkdArr.map(item =>{
  let id = answer.findIndex(it =>it.itemId === item.itemId);
  if(id === - 1 ){
    answer.push({itemId: item.itemId, data: [{date: item.date, quantity: item.quantity}]})
  }else{
    answer [id] = {itemId: item.itemId, data: [...answer[id].data, {date: item.date, quantity: item.quantity}]}
  }
})

console.log(answer)
about 4 years ago · Juan Pablo Isaza Report

0

This can be done efficiently using a map, and a new output array. In the map, we will store the key as itemId and value as its index in the output array. Then we will loop through the data, and see if the itemId has an index in the map, if it is we will look up that item value from output array, and update the current date value accordingly, otherwise we will create a new object for that item in the array. The below code does just that.

const data = [
  {
    date: "2022-05-25",
    itemId: 29,
    quantity: 4,
  },
  {
    date: "2022-05-26",
    itemId: 29,
    quantity: 2,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 7,
  },
  {
    date: "2022-05-25",
    itemId: 36,
    quantity: 4,
  },
  {
    date: "2022-05-27",
    itemId: 36,
    quantity: 2,
  },
  {
    date: "2022-05-28",
    itemId: 47,
    quantity: 1,
  },
];

let output = [];
let itemIdToIndexMap = {};

data.forEach(item => {
    const itemIndex = itemIdToIndexMap[item.itemId];
    if(itemIndex !== undefined) {
         const itemArray = output[itemIndex];
         const itemWithCurrentDate = itemArray.date.find(newItem => item.date === newItem.date);
         if(itemWithCurrentDate) {
             itemWithCurrentDate.quantity += item.quantity; 
         } else {
             itemArray.date.push({date: item.date, quantity: item.quantity});
         }
    } else {
        output.push({itemId: item.itemId, date: [{date: item.date, quantity: item.quantity}]});
        itemIdToIndexMap[item.itemId] = output.length - 1;
    }
});
console.log(JSON.stringify(output));
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!