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

221
Views
Using reduce array method to sum up (increment) records properties results with an undefined error

I found tons of questions related to this issue, but still couldn't figure out what's wrong with my code using .reduce() array method.

Consider this simple toy data:

const myRecords = [
  { name: "john", preferredColor: "green" },
  { name: "diana", preferredColor: "red" },
  { name: "george", preferredColor: "yellow" },
  { name: "ron", preferredColor: "green" },
  { name: "sarah", preferredColor: "red" },
  { name: "rachel", preferredColor: "red" },
  { name: "nicole", preferredColor: "yellow" },
  { name: "chris", preferredColor: "red" },
];

I want to summarize this data into an object that sums up each color:

{ colorCount: { red: number, yellow: number, green: number } }

To this end, reduce seems a natural choice. Here's my implementation that doesn't work:

// my attempt
const res = myRecords.reduce(
  (acc, person) => // reducer
    person.preferredColor === "green"
      ? (acc.colorCount.green += 1)
      : person.preferredColor === "yellow"
      ? (acc.colorCount.yellow += 1)
      : (acc.colorCount.red += 1), 
  { colorCount: { red: 0, yellow: 0, green: 0 } } // init value
);

But I get this error

Cannot read properties of undefined (reading 'red') 

I can't wrap my head around this. My code seems pretty straightforward. I'm not missing a return statement because the reducer function is an arrow function without {} scope. Where's the mistake?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I saw your code, and you should return the acc in the reduce. I created another solution version and incremented the corresponding color counter based on the preferredColor value. You can check it out here:

const myRecords = [
  { name: "john", preferredColor: "green" },
  { name: "diana", preferredColor: "red" },
  { name: "george", preferredColor: "yellow" },
  { name: "ron", preferredColor: "green" },
  { name: "sarah", preferredColor: "red" },
  { name: "rachel", preferredColor: "red" },
  { name: "nicole", preferredColor: "yellow" },
  { name: "chris", preferredColor: "red" },
];

const result = myRecords.reduce((prev, curr) => ({
  ...prev,
  colorCount: {
    ...prev.colorCount,
    [curr.preferredColor]: prev.colorCount[curr.preferredColor] + 1
  }
}), { colorCount: { red: 0, yellow: 0, green: 0 } })

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

I don't think you can get the result you want using the reduce method but the bellow code works.

const myRecords = [
    { name: "john", preferredColor: "green" },
    { name: "diana", preferredColor: "red" },
    { name: "george", preferredColor: "yellow" },
    { name: "ron", preferredColor: "green" },
    { name: "sarah", preferredColor: "red" },
    { name: "rachel", preferredColor: "red" },
    { name: "nicole", preferredColor: "yellow" },
    { name: "chris", preferredColor: "red" },
  ];

    const result = { colorCount: { red: 0, yellow: 0, green: 0 } };

  myRecords.forEach(rec => {
    rec.preferredColor === "green"
        ? (result.colorCount.green += 1)
        : rec.preferredColor === "yellow"
        ? (result.colorCount.yellow += 1)
        : (result.colorCount.red += 1);
  });

  console.log(result);

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!