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

99
Views
Count unique instances of object property and tally based on other properties

I need to count the amount of times DishName appears in an array of objects, whilst also counting which are cooked and which are not, with the value of 1 being cooked and 0 being uncooked. So the following:

[{
    "Cooked": 1,
    "DishName": "85. Tikka Masala"
},
{
    "Cooked": 0,
    "DishName": "89. Lamb Rezalla"
},
{
    "Cooked": 1,
    "DishName": "85. King Prawn Masala"
},
{
    "Cooked": 0,
    "DishName": "85. Tikka Masala"
}]

Should return:


DishName -- Total Quantity -- Cooked -- Uncooked


  1. Tikka Masala -- 2 -- 1 -- 1
  2. Lamb Rezalla -- 1 -- 0 -- 1
  3. King Prawn Masala -- 1 -- 1 -- 0
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is a typical case for reduce. Reduce to an object that's keyed by the dish names. For each, record the total count and the counts of the other conditions (cooked / uncooked). Transform that result into an array of strings by mapping object entires.

let orders = [{
    "Cooked": 1,
    "DishName": "85. Tikka Masala"
},
{
    "Cooked": 0,
    "DishName": "89. Lamb Rezalla"
},
{
    "Cooked": 1,
    "DishName": "85. King Prawn Masala"
},
{
    "Cooked": 0,
    "DishName": "85. Tikka Masala"
}];

let summary = orders.reduce((acc, order) => {
  if (!acc[order.DishName]) acc[order.DishName] = { count: 0, cooked: 0, uncooked: 0 };
  acc[order.DishName].count++;
  order.Cooked ? acc[order.DishName].cooked++ : acc[order.DishName].uncooked++;
  return acc;
}, {});

// this produces:
// { "85. Tikka Masala" : { count: 2, cooked: 1, uncooked: 1 }}, ...

// formatted just like the OP...
let printReady = Object.entries(summary).map(([name, s]) => `${name} -- ${s.count} -- ${s.cooked} -- ${s.uncooked}`);
 console.log(printReady);

about 4 years ago · Juan Pablo Isaza Report

0

const array = [{
    "Cooked": 1,
    "DishName": "85. Tikka Masala"
},
{
    "Cooked": 0,
    "DishName": "89. Lamb Rezalla"
},
{
    "Cooked": 1,
    "DishName": "85. King Prawn Masala"
},
{
    "Cooked": 0,
    "DishName": "85. Tikka Masala"
}];

const result = {};
array
.map(item => {
  if (!result[item['DishName']]) {
    result[item['DishName']] = {
      quantity: 0,
      cooked: 0,
      uncooked: 0
    }
  } 
  
  result[item['DishName']].quantity++;
  result[item['DishName']][item['Cooked'] ? 'cooked' : 'uncooked']++;
})
.sort((a, b) => a.quantity - b.quantity);

for ([key, value] of Object.entries(result)) {
  console.log(`${key}--${value.quantity}--${value.cooked}--${value.uncooked}`);
}

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!