javascriptcountmapreducereducearray-reduce

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!


Solution

  • 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)