How does one recursively count the maximum nesting occurrence of a specific, itself repeating, sub data structure which is an array of object items?

Hi i get object with my backend, looks like that:

 reaction {[
          some data about reaction ,
          reactions: {[
                     some data about reaction ,
                     reactions: {[
                                 some data about comment,
                                 reactions: 
                                ]}
                    ]}
          ]}

Nested reactions can be infinity, and there can be any number of reactions in the reaction.

I try count all reactions:

reaction.flat(Infinity).length

also i try:

const getArrayDepth = arr => {
    if (Array.isArray(arr)) {
      return 1 + Math.max(...arr.map(getArrayDepth));
    }
    if (arr.reactions&& arr.reactions.length) {
      return 1 + Math.max(...arr.reactions.map(getArrayDepth));
    }
    return 0;
  };

and

const totalReactions  = arr => arr.reduce((count, current) => count + current.reactions.length, 0);

but I get an incorrect number, can anyone tell me what I am doing wrong, I would be very grateful?

Answer

function getMaximumReactionsFoldCount(reactions, foldCount = 0) {
  return reactions.reduce((count, item) => {
    if (item.hasOwnProperty('reactions')) {

      count = Math.max(
        count,
        getMaximumReactionsFoldCount(item.reactions, ++foldCount)
      );
    }
    return count;

  }, foldCount);
}

const reactions = [{              // 1
  foo: 'foo',
  bar: 'bar',
  reactions: [{                   // 1a-2
    fooBar: 'foo-bar',
    bazBiz: 'bar-biz',
    reactions: [{                 // 1a-2-3
      foo: 'foo',
      bar: 'bar',
      reactions: [{               // 1a-2-3-4
        // ... more ...
      }],
    }],
    buzBoz: 'buz-boz',
  }],
  baz: 'baz',
}, {
  fooFoo: 'foo-foo',
  barBar: 'bar-bar',
  reactions: [{                   // 1b-2
    fooBarBaz: 'foo-bar-baz',
    reactions: [{                 // 1b-2-3
      foo: 'foo',
      bar: 'bar',
      reactions: [{               // 1b-2-3-4a
        fooBar: 'foo-bar',
        bazBiz: 'bar-biz',
        reactions: [{             // 1b-2-3-4a-5
          foo: 'foo',
          bar: 'bar',
          reactions: [{           // 1b-2-3-4a-5-6
            reactions: [{         // 1b-2-3-4a-5-6-7
              reactions: [{       // 1b-2-3-4a-5-6-7-8
                // ... more ...
              }],
              bizzz: 'BIZZZ',
            }],
            buzzz: 'BUZZZ',
          }],
          bozzz: 'BOZZZ',
        }],
        buzBoz: 'buz-boz',
      }],
      baz: 'baz',
    }, {
      fooFoo: 'foo-foo',
      barBar: 'bar-bar',
      reactions: [{               // 1b-2-3-4b
        fooBarBaz: 'foo-bar-baz',
        reactions: [              // 1b-2-3-4b-5
          // ... more ...
        ],
        bizBuzBoz: 'biz-buz-boz',
      }],
      bazBaz: 'baz-baz',
    }],
    bizBuzBoz: 'biz-buz-boz',
  }],
  bazBaz: 'baz-baz',
}];

console.log(
  'getMaximumReactionsFoldCount(reactions) ...',
  getMaximumReactionsFoldCount(reactions)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }