Search code examples
javascriptjavascript-objects

Counting the number of leaves in a JavaScript Object


I have an errors object in the following way and I want to count the total number of leaves, my object is as follows

I have tried using the object.keys() property but it only counts the direct children of the object and not the nested values

state = {
    driver: {
        user: {
            number: '1234',
            firstName: 'xyz',
            lastName: 'abc'
        },
        kyc: {
            name: 'kyc1',
            idNumber: 'kyc2'
        },
        license: {
            idNumber: '1234',
            issuedOn: '1991-05-12',
            validTill: '1991-12-12'
        }
    },
    vehicle: {
        vin: '4567',
        registrationNumber: '4567',
        dateBought: '1954-09-12',
        contractValidity: '1954-12-12',
        departments: [1, 2],
        vehicleType: 2
    },
    owner: {
        user: {
            number: '1234',
            firstName: 'lml',
            lastName: 'pqr',
            dateOfBirth: '2068-11-26'
        },
        kyc: {
            name: 'kyc2',
            idNumber: 'kyc123'
        },
        bank: {
            ifscCode: '1234',
            bankName: 'bank1',
            branchName: 'branch1',
            accountNumber: '1234',
            accountHolderName: 'holder1'
        },
        license: {
            idNumber: '12345',
            issuedOn: '1997-12-12',
            validTill: '1997-12-12'
        },
        insurance: {
            idNumber: '1234',
            issuedOn: '2909-12-12',
            validTill: '2909-12-12'
        }
    },
    errors: {},
    isSubmitable: false
}

Solution

  • You can take a recursive approach and count each value that is not an object (but count arrays as a single leaf), otherwise get all values of the object and try to count them:

    const state = { driver: { user: { number: '1234', firstName: 'xyz', lastName: 'abc' }, kyc: { name: 'kyc1', idNumber: 'kyc2' }, license: { idNumber: '1234', issuedOn: '1991-05-12', validTill: '1991-12-12' } }, vehicle: { vin: '4567', registrationNumber: '4567', dateBought: '1954-09-12', contractValidity: '1954-12-12', departments: [1, 2], vehicleType: 2 }, owner: { user: { number: '1234', firstName: 'lml', lastName: 'pqr', dateOfBirth: '2068-11-26' }, kyc: { name: 'kyc2', idNumber: 'kyc123' }, bank: { ifscCode: '1234', bankName: 'bank1', branchName: 'branch1', accountNumber: '1234', accountHolderName: 'holder1' }, license: { idNumber: '12345', issuedOn: '1997-12-12', validTill: '1997-12-12' }, insurance: { idNumber: '1234', issuedOn: '2909-12-12', validTill: '2909-12-12' } }, errors: {}, isSubmitable: false}
    
    function countLeaves(val) {
      if (typeof val !== "object" || Array.isArray(val)) {
        return 1;
      }
      
      return Object.values(val).map(countLeaves).reduce(sum, 0)
    }
    
    const sum = (a, b) => a + b;
    
    console.log(countLeaves(state));