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

205
Views
Count bottom-level elements in nested object of objects of arrays

I have an "object of objects of arrays" and I simply need to get the total count of elements in the "bottom level" arrays ...so with the example below, I need to return 19 (counting elements a to s):

var arr={ 'AA': { 'aa': ['a','b'],
                  'bb': ['c','d'],
                  'cc': ['e','f'],
                  'dd': ['g','h'] }, 
          'BB': { 'ee': ['i','j'],
                  'ff': ['k','l'] }, 
          'CC': { 'gg': ['m','n'],
                  'hh': ['o','p'],
                  'ii': ['q','r','s'] } };

I have it working with this but it ain't pretty:

var cnt=0;
for(var f0 in arr){
  for(var f1 in arr[f0]){
    cnt+=arr[f0][f1].length;
  }
}

It's probably best to use map and reduce but can't quite wrap my head around it. There are similar Q+A's (like this) but I couldn't quite find one that fits this situation.

Thanks!

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

0

By using Object.values to access the values associated with each key, you can use a nested reduce to count all the bottom level elements:

var arr={ 'AA': { 'aa': ['a','b'],
                  'bb': ['c','d'],
                  'cc': ['e','f'],
                  'dd': ['g','h'] }, 
          'BB': { 'ee': ['i','j'],
                  'ff': ['k','l'] }, 
          'CC': { 'gg': ['m','n'],
                  'hh': ['o','p'],
                  'ii': ['q','r','s'] } };
                  
const cnt = Object.values(arr)
  .reduce((c, o) => c + Object.values(o)
    .reduce((c, a) => c + a.length,
            0),
          0);
  
console.log(cnt)

  

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!