计算JavaScript对象中的叶子数

时间:2019-05-30 06:43:33

标签: javascript javascript-objects

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

我尝试使用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
}

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

let num = 0;
const objectLength = (obj) => {
    for (let [key, value] of Object.entries(obj)) {
        if (typeof value == 'object') {
            num++;
            objectLength(value);
        } else if (obj.hasOwnProperty(key)) {
            num++;
        }
    }
}

这将计算所有元素,甚至包括嵌套元素。