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

162
Views
Counting the occurrences of an object in an array of objects based on a key

I have an array of objects as follows :

let people = [
              { name: "Emily", age: 15 }, { name: "Emma", age: 16 }, 
              { name: "Stacy", age: 18 }, { name: "Emily", age: 15 },
              { name: "Jennifer", age: 12 }  
             ];

I need to return the result containing the age as the key and frequency as it's corresponding value as follows :

{ 15 : 2, 16 : 1, 18 : 1, 12 : 1 }

I wish I could implement this using both forEach() and reduce().

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

0

Using Array#reduce:

const people = [ { name: "Emily", age: 15 }, { name: "Emma", age: 16 }, { name: "Stacy", age: 18 }, { name: "Emily", age: 15 }, { name: "Jennifer", age: 12 } ];

const ageCount = people.reduce((map, { age }) => ({
  ...map,
  [age]: (map[age] || 0) + 1
}), {});

console.log(ageCount);

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!