Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda