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

95
Visualizações
Totalling Array of objects values using reducer without using find

I am reducing the data array down to one entry per DishName with Qty, Cooked and NotCooked values added together if the DishNames are the same.

What I have works, however due to needing compatibility with old devices I cannot use .find() with "fat arrows". I've tried racking my brain but cannot come up with a solution.

var data = [
  { DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '38. King Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: 'Chicken Tikka Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
];

var res = data.reduce(function(acc, obj) {
  var existItem = acc.find(item => item.DishName === obj.DishName);

  if (existItem) {
    existItem.Qty += obj.Qty;
    existItem.Cooked += obj.Cooked;
    existItem.NotCooked += obj.NotCooked;
    return acc;
  }
  acc.push(obj);
  return acc;
}, []);

console.log(res);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The var existItem = acc.find(item => item.DishName === obj.DishName); can be rewritten without arrow function as

var existItem = acc.find(function(item){return item.DishName === obj.DishName;});

Or if you want to avoid using find as

var existItem = acc.filter(function(item){return item.DishName === obj.DishName;})[0];

But i would also change acc.push(obj); to acc.push({...obj}); to avoid mutating the original data array.

about 4 years ago · Juan Pablo Isaza Relatório

0

Arrow functions are slightly different than regular functions but are very similar. In your case, you can just use find with a regular function.

var data =
      [
         {DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0},
         {DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0},
         {DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0},
         {DishName: '38. King Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0},
         {DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1},
         {DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1},
         {DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1},
         {DishName: 'Chicken Tikka Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1},
      ]


var res = data.reduce(function(acc, obj) {
  var existItem = acc.find(function(item){return item.DishName === obj.DishName});

  if (existItem) {
    existItem.Qty += obj.Qty;
    existItem.Cooked += obj.Cooked;
    existItem.NotCooked += obj.NotCooked;
    return acc;
  }
  acc.push(obj);
  return acc;
}, []);

console.log(res);

about 4 years ago · Juan Pablo Isaza Relatório

0

An approach free of find has the additional advantage of saving for each step of a full reduce cycle the unnecessary find iteration.

A solution based on an accumulator which does take care of mapping and collecting un/merged dish-items runs just a single reduce cycle.

function mergeSameDish(collector, item) {
  var index = collector.index;
  var list = collector.list;

  var merger = index[item.DishName];
  if (merger) {
    // a (to be) merged dish item.
    merger.Qty = merger.Qty + item.Qty;
    merger.Cooked = merger.Cooked + item.Cooked;
    merger.NotCooked = merger.NotCooked + item.NotCooked;

  } else {
    // collect and map a shallow dish item copy
    // in order to not later mutate the original.
    list.push(
      index[item.DishName] = Object.assign({}, item)
    );
  }
  return collector;
}

var data = [
  { DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '38. King Prawn Vindaloo', Qty: 1, Cooked: 1, NotCooked: 0 },
  { DishName: '35. Chicken Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: '36. Lamb Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: '37. Prawn Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
  { DishName: 'Chicken Tikka Vindaloo', Qty: 1, Cooked: 0, NotCooked: 1 },
];

console.log(
  'mutated/reduced items ...',
  data.reduce(mergeSameDish, {
    index: {},
    list: [],
  }).list
);
console.log('unchanged/original items ... ', data);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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