计算JavaScript对象中的叶子数

 收藏

我以下面的方式有一个错误对象,我想计算叶子的总数,我的对象如下

我试过使用object.keys()属性,但它仅计算对象的直接子级,而不计算嵌套值

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
}
回复
  • 亦莫离 回复

    您可以采用递归方法,对不是对象的每个值进行计数(但将数组作为单个叶计数),否则获取对象的所有值并尝试对它们进行计数:

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

  • 情侣 回复

    这完全取决于我们如何定义叶子,但是使用递归,类似的方法应该可以工作:

    function countLeaves(obj) {
        return Object.values(obj).reduce((count,v) => count += (typeof(v) === 'object') ? countLeaves(v): 1, 0);
    }
    
    let 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 };
    
    console.log("Number of leaves: ", countLeaves(state));