Bhart Supriya
Bhart Supriya

Reputation: 262

Find length of a nested array's object children

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of all the children , here in this case for the first object in array it should be one and for the second object two , and the total comes down to 3.

const testData = [
    {
        account: "A",
        children: [
            {
                account: "Test",
                children: [],
            },
        ],
    }, {
        account: "B",
        children: [
            {
                account: "Test1",
                children: [],
            },
            {
                account: "Test2",
                children: [],
            },
        ],
    },

]

Upvotes: 1

Views: 1381

Answers (3)

Jamiec
Jamiec

Reputation: 136124

The recursive solution, should you need it

const testData = [
    {
        account: "A",
        children: [
            {
                account: "Test",
                children: [{account:"Test-Grandchild", children:[{account:"Test-Great-Grandchild", children:[]}]}],
            },
        ],
    }, {
        account: "B",
        children: [
            {
                account: "Test1",
                children: [],
            },
            {
                account: "Test2",
                children: [],
            },
        ],
    },

]

const countChildren = obj => obj.children.length + obj.children.reduce((acc,c) => acc + countChildren(c), 0 );
const result = testData.reduce ( (acc,c) => acc + countChildren(c),0)
console.log(result);

Upvotes: 2

Muhammed Bera Koç
Muhammed Bera Koç

Reputation: 416

I think Kooilnic's answer perfect for the situation since we are reducing the array into a single number value and it should be accepted as the actual answer. But I want to propose another solution using Array.forEach:

const testData = [
    {
        account: "A",
        children: [
            {
                account: "Test",
                children: [],
            },
        ],
    }, {
        account: "B",
        children: [
            {
                account: "Test1",
                children: [],
            },
            {
                account: "Test2",
                children: [],
            },
        ],
    },

]
let numberOfChildren = 0
testData.forEach(datum => numberOfChildren += datum.children.length)
console.log({ numberOfChildren })

Upvotes: 2

KooiInc
KooiInc

Reputation: 122916

Sure, Array.reduce

const nOfTestDataChildren = [
    {
        account: "A",
        children: [
            {
                account: "Test",
                children: [],
            },
        ],
    }, {
        account: "B",
        children: [
            {
                account: "Test1",
                children: [],
            },
            {
                account: "Test2",
                children: [],
            },
        ],
    },
].reduce( (acc, val) => acc + val.children.length, 0);

console.log(nOfTestDataChildren);

Upvotes: 2

Related Questions