skip to Main Content

The user has children, children also have children as in the code example. There are a maximum of 5 such levels, but maybe less, 2, 3. My task is to iterate each level and return the number of children the user has at each level. I have been unable to solve this problem for two days now. help me please

"children": [
        {
            "id": "7b3602fa-0df3-4170-9248-9a5df00b0bd7",
            "name": "Rick Rich",
            "email": "[email protected]",
            "role": "user",
            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiN2IzNjAyZmEtMGRmMy00MTcwLTkyNDgtOWE1ZGYwMGIwYmQ3IiwiaWF0IjoxNjg1NDQ0Mzg0LCJleHAiOjMzMjExNDg2Nzg0fQ.4sFrfYjepY4Yk0FvUjDPoKiIw6sBrrmC7gYQPKlgCZo",
            "walletAmount": 0,
            "deposit": 0,
            "bonus": 100,
            "referralChildDeposit": 0,
            "children": [
                {
                    "id": "ddf86d60-a607-4a4e-a7f9-d96013ee7070",
                    "name": "Rick Rich",
                    "email": "[email protected]",
                    "role": "user",
                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZGRmODZkNjAtYTYwNy00YTRlLWE3ZjktZDk2MDEzZWU3MDcwIiwiaWF0IjoxNjg1NDQ0NDYzLCJleHAiOjMzMjExNDg2ODYzfQ.LUtlnL-j5KVjTgLCkzV47Txt9nyxpt108CBDgEMYdKY",
                    "walletAmount": 0,
                    "deposit": 0,
                    "bonus": 100,
                    "referralChildDeposit": 0,
                    "children": [
                        {
                            "id": "25de2e98-eb2d-41f4-b225-3069f942b284",
                            "name": "Rick Rich",
                            "email": "[email protected]",
                            "role": "user",
                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMjVkZTJlOTgtZWIyZC00MWY0LWIyMjUtMzA2OWY5NDJiMjg0IiwiaWF0IjoxNjg1NDQ0NTA4LCJleHAiOjMzMjExNDg2OTA4fQ.EfPLd4A1loiC36UVAPeF4zjhyAqUsXJxxahpQsvxS3A",
                            "walletAmount": 0,
                            "deposit": 0,
                            "bonus": 100,
                            "referralChildDeposit": 0,
                            "children": [
                                {
                                    "id": "376b202e-d44f-4402-9560-8498c855d05e",
                                    "name": "Rick Rich",
                                    "email": "[email protected]",
                                    "role": "user",
                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMzc2YjIwMmUtZDQ0Zi00NDAyLTk1NjAtODQ5OGM4NTVkMDVlIiwiaWF0IjoxNjg1NDQ0NTcyLCJleHAiOjMzMjExNDg2OTcyfQ.b3AvdAo9vuIzk0kCU2mPts8W5mfLMba78LF9SrW7en4",
                                    "walletAmount": 0,
                                    "deposit": 0,
                                    "bonus": 100,
                                    "referralChildDeposit": 0,
                                    "children": [
                                        {
                                            "id": "042681be-f521-40af-a1dd-a1591700f8e0",
                                            "name": "Rick Rich",
                                            "email": "[email protected]",
                                            "role": "user",
                                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMDQyNjgxYmUtZjUyMS00MGFmLWExZGQtYTE1OTE3MDBmOGUwIiwiaWF0IjoxNjg1NDQ0NjY4LCJleHAiOjMzMjExNDg3MDY4fQ.OTQ71d_mGw4wp4rQIStZPG0lKsZz0-NxgZnxjQ4cDgY",
                                            "walletAmount": 0,
                                            "deposit": 0,
                                            "bonus": 100,
                                            "referralChildDeposit": 0,
                                            "children": []
                                        },
                                        {
                                            "id": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                            "name": "Rick Rich",
                                            "email": "[email protected]",
                                            "role": "user",
                                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOTk3ZTAyYTctMWJiNy00NzNmLThmZjgtNWViNzZkOGQzMDliIiwiaWF0IjoxNjg1NDQ0NjcyLCJleHAiOjMzMjExNDg3MDcyfQ.zD4TxMGTxtwQozzXmkUNHIkYvqgWWONkLW-mP1D446k",
                                            "walletAmount": 0,
                                            "deposit": 0,
                                            "bonus": 100,
                                            "referralChildDeposit": 0,
                                            "children": [
                                                {
                                                    "id": "4195f2b0-2603-414d-96f5-2099e4fcfdb1",
                                                    "email": "[email protected]",
                                                    "isActivate": false,
                                                    "name": "Rick Rich",
                                                    "password": "$2b$10$BcwivMHVzAEe8/.vXxe.neZivaieoNNgX7iZ1i7bspXCXjbJZZMqi",
                                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                    "role": "user",
                                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiNDE5NWYyYjAtMjYwMy00MTRkLTk2ZjUtMjA5OWU0ZmNmZGIxIiwiaWF0IjoxNjg1NDQ0NzI0LCJleHAiOjMzMjExNDg3MTI0fQ.128GQLG0jU-2Tu-DwNJMD_EbUIUwtYcrTDXWfRbGdcY",
                                                    "referralChildDeposit": 0,
                                                    "referralBloggerChildDeposit": 0,
                                                    "walletAmount": 0,
                                                    "deposit": 0,
                                                    "bonus": 100,
                                                    "gemstone": 0,
                                                    "frozen": false,
                                                    "banned": false,
                                                    "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "createdAt": "2023-05-30T11:05:24.334Z",
                                                    "updatedAt": "2023-05-30T11:05:24.341Z"
                                                },
                                                {
                                                    "id": "f427ac9b-c7d5-49ef-a91f-722ac8d40d93",
                                                    "email": "[email protected]",
                                                    "isActivate": false,
                                                    "name": "Rick Rich",
                                                    "password": "$2b$10$UXnr0uGxTs9UW6wcckKiMuBvcOTsLwD1YtE.J/tS5PK8RoaTvO9Ce",
                                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                    "role": "user",
                                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZjQyN2FjOWItYzdkNS00OWVmLWE5MWYtNzIyYWM4ZDQwZDkzIiwiaWF0IjoxNjg1NDQ0NzI4LCJleHAiOjMzMjExNDg3MTI4fQ.F-2gl4pLIzp40VzehSP5X44aDLQE-SFEfb3GoJuc4MU",
                                                    "referralChildDeposit": 0,
                                                    "referralBloggerChildDeposit": 0,
                                                    "walletAmount": 0,
                                                    "deposit": 0,
                                                    "bonus": 100,
                                                    "gemstone": 0,
                                                    "frozen": false,
                                                    "banned": false,
                                                    "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "createdAt": "2023-05-30T11:05:28.471Z",
                                                    "updatedAt": "2023-05-30T11:05:28.478Z"
                                                },
                                                {
                                                    "id": "c063bea1-37d0-4173-805f-6ad1d7b628b8",
                                                    "email": "[email protected]",
                                                    "isActivate": false,
                                                    "name": "Rick Rich",
                                                    "password": "$2b$10$CT7xqTZQSE/G1KSiwDgx4.phGXBDreIKp29ZAK.EyRykn.nl3/oiq",
                                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                    "role": "user",
                                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYzA2M2JlYTEtMzdkMC00MTczLTgwNWYtNmFkMWQ3YjYyOGI4IiwiaWF0IjoxNjg1NDQ0NzMzLCJleHAiOjMzMjExNDg3MTMzfQ.8L1ds9YvJ3_SH1ENtq8epYOYT4Wj_jnIvaGYIvoSviM",
                                                    "referralChildDeposit": 0,
                                                    "referralBloggerChildDeposit": 0,
                                                    "walletAmount": 0,
                                                    "deposit": 0,
                                                    "bonus": 100,
                                                    "gemstone": 0,
                                                    "frozen": false,
                                                    "banned": false,
                                                    "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "createdAt": "2023-05-30T11:05:33.318Z",
                                                    "updatedAt": "2023-05-30T11:05:33.325Z"
                                                },
                                                {
                                                    "id": "169f5ad5-0405-42b6-9ce5-a50ca9d9e723",
                                                    "email": "[email protected]",
                                                    "isActivate": false,
                                                    "name": "Rick Rich",
                                                    "password": "$2b$10$CYPOO2BQydNdyt5/3eLXSeBam4nqlXcCMg4cKdDE6q.56OIewBw9y",
                                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                    "role": "user",
                                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMTY5ZjVhZDUtMDQwNS00MmI2LTljZTUtYTUwY2E5ZDllNzIzIiwiaWF0IjoxNjg1NDUzMjYzLCJleHAiOjMzMjExNDk1NjYzfQ.SxnCWoWRZ7uNho7Jf7orpG2Tcyv9Mu5gzxKcAZ8bpKc",
                                                    "referralChildDeposit": 0,
                                                    "referralBloggerChildDeposit": 0,
                                                    "walletAmount": 0,
                                                    "deposit": 0,
                                                    "bonus": 100,
                                                    "gemstone": 0,
                                                    "frozen": false,
                                                    "banned": false,
                                                    "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                    "createdAt": "2023-05-30T13:27:43.076Z",
                                                    "updatedAt": "2023-05-30T13:27:43.085Z"
                                                }
                                            ]
                                        },
                                        {
                                            "id": "b969c5b8-2d01-4872-8063-8e9ca7474a13",
                                            "name": "Rick Rich",
                                            "email": "[email protected]",
                                            "role": "user",
                                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYjk2OWM1YjgtMmQwMS00ODcyLTgwNjMtOGU5Y2E3NDc0YTEzIiwiaWF0IjoxNjg1NDQ0Njc3LCJleHAiOjMzMjExNDg3MDc3fQ.IeAuHU8cNTehSPdXlPuFN-BeG_Fk6nwavnrCOa48lMM",
                                            "walletAmount": 0,
                                            "deposit": 0,
                                            "bonus": 100,
                                            "referralChildDeposit": 0,
                                            "children": []
                                        }
                                    ]
                                },
                                {
                                    "id": "d8252898-46b8-4cf4-a545-06e98e6dccf1",
                                    "name": "Rick Rich",
                                    "email": "[email protected]",
                                    "role": "user",
                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZDgyNTI4OTgtNDZiOC00Y2Y0LWE1NDUtMDZlOThlNmRjY2YxIiwiaWF0IjoxNjg1NDQ0NTc2LCJleHAiOjMzMjExNDg2OTc2fQ.Ox6sV1vfBemE33bwzFLiVmMiTOioBz_xxc968L81wEo",
                                    "walletAmount": 0,
                                    "deposit": 0,
                                    "bonus": 100,
                                    "referralChildDeposit": 0,
                                    "children": []
                                },
                                {
                                    "id": "0d6ae01b-ccda-4de3-83ca-d2f70dd0897c",
                                    "name": "Rick Rich",
                                    "email": "[email protected]",
                                    "role": "user",
                                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMGQ2YWUwMWItY2NkYS00ZGUzLTgzY2EtZDJmNzBkZDA4OTdjIiwiaWF0IjoxNjg1NDQ0NTgxLCJleHAiOjMzMjExNDg2OTgxfQ._M-lVXD7zxt5BRdhiIm2pCPJ4Yaikt-_eBGowlR8InU",
                                    "walletAmount": 0,
                                    "deposit": 0,
                                    "bonus": 100,
                                    "referralChildDeposit": 0,
                                    "children": []
                                }
                            ]
                        },
                        {
                            "id": "0ee1ea8f-790f-4767-b280-c5cddfe9c630",
                            "name": "Rick Rich",
                            "email": "[email protected]",
                            "role": "user",
                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMGVlMWVhOGYtNzkwZi00NzY3LWIyODAtYzVjZGRmZTljNjMwIiwiaWF0IjoxNjg1NDQ0NTE1LCJleHAiOjMzMjExNDg2OTE1fQ.pis1sqBc6DZT1_INLEtKBdx_htymiLRiNE9obXzJ1B0",
                            "walletAmount": 0,
                            "deposit": 0,
                            "bonus": 100,
                            "referralChildDeposit": 0,
                            "children": []
                        },
                        {
                            "id": "eb530dbf-9026-4cb8-a3fa-d02133ced42d",
                            "name": "Rick Rich",
                            "email": "[email protected]",
                            "role": "user",
                            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZWI1MzBkYmYtOTAyNi00Y2I4LWEzZmEtZDAyMTMzY2VkNDJkIiwiaWF0IjoxNjg1NDQ0NTIwLCJleHAiOjMzMjExNDg2OTIwfQ.3FVnsBwylesvv8Lf9Uyb4cA1d1399JFie-6F__6-nr0",
                            "walletAmount": 0,
                            "deposit": 0,
                            "bonus": 100,
                            "referralChildDeposit": 0,
                            "children": []
                        }
                    ]
                },
                {
                    "id": "d1022d3c-25af-461c-af91-cbc64147f92c",
                    "name": "Rick Rich",
                    "email": "[email protected]",
                    "role": "user",
                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZDEwMjJkM2MtMjVhZi00NjFjLWFmOTEtY2JjNjQxNDdmOTJjIiwiaWF0IjoxNjg1NDQ0NDczLCJleHAiOjMzMjExNDg2ODczfQ.49qPRgY8DW1M8za7WY51LepIzhtVG_863p7l4CJFLRg",
                    "walletAmount": 0,
                    "deposit": 0,
                    "bonus": 100,
                    "referralChildDeposit": 0,
                    "children": []
                },
                {
                    "id": "5927f160-e4ad-4b13-a47e-b1d4b0687897",
                    "name": "Rick Rich",
                    "email": "[email protected]",
                    "role": "user",
                    "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                    "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiNTkyN2YxNjAtZTRhZC00YjEzLWE0N2UtYjFkNGIwNjg3ODk3IiwiaWF0IjoxNjg1NDQ0NDc4LCJleHAiOjMzMjExNDg2ODc4fQ.7nf966SGK-umk4LgjF2AMvzcznRS1vg7akLjYcHAXb4",
                    "walletAmount": 0,
                    "deposit": 0,
                    "bonus": 100,
                    "referralChildDeposit": 0,
                    "children": []
                }
            ]
        },
        {
            "id": "8dc79a93-3893-45ca-ba15-3bb3c1e7061d",
            "name": "Rick Rich",
            "email": "[email protected]",
            "role": "user",
            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOGRjNzlhOTMtMzg5My00NWNhLWJhMTUtM2JiM2MxZTcwNjFkIiwiaWF0IjoxNjg1NDQ0MzkwLCJleHAiOjMzMjExNDg2NzkwfQ.KH81VuIHXUqGrq68rB7b-lTrpfHU8MnsUMo96a83Rus",
            "walletAmount": 0,
            "deposit": 0,
            "bonus": 100,
            "referralChildDeposit": 0,
            "children": []
        },
        {
            "id": "9ded36ef-ac0f-42a4-b46f-58ca522d0a71",
            "name": "Rick Rich",
            "email": "[email protected]",
            "role": "user",
            "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
            "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOWRlZDM2ZWYtYWMwZi00MmE0LWI0NmYtNThjYTUyMmQwYTcxIiwiaWF0IjoxNjg1NDQ0Mzk2LCJleHAiOjMzMjExNDg2Nzk2fQ.ie03HNsGdWiDRe_m7EBNvp31ygqhVwfPGqtyvlOTc-c",
            "walletAmount": 0,
            "deposit": 0,
            "bonus": 100,
            "referralChildDeposit": 0,
            "children": []
        }
    ],

3

Answers


  1. i think this may be what you are looking for?

    result = [];
    function recurse(level, obj) {
        if(obj.children) {
            result[level] = obj.children.length + (result[level] === undefined ? 0 : result[level]);
            for(i in obj.children)
                recurse((level + 1), obj.children[i]);
        }
    }
    recurse(0, data);
    console.log(result);
    
    Login or Signup to reply.
  2. You could use a DFS algorithm to iterate all children notes with recursion:

    const count = (children, arr = [], level = 0) => {
        children.forEach(node => {
            arr[level] = (arr[level] || 0) + 1;
            if (node.children) {
                count(node.children, arr, level + 1);
            }
        })
    
        return arr;
    };
    
    const output = count(data.children);
    
    const data = {
        "children": [
            {
                "id": "7b3602fa-0df3-4170-9248-9a5df00b0bd7",
                "name": "Rick Rich",
                "email": "[email protected]",
                "role": "user",
                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiN2IzNjAyZmEtMGRmMy00MTcwLTkyNDgtOWE1ZGYwMGIwYmQ3IiwiaWF0IjoxNjg1NDQ0Mzg0LCJleHAiOjMzMjExNDg2Nzg0fQ.4sFrfYjepY4Yk0FvUjDPoKiIw6sBrrmC7gYQPKlgCZo",
                "walletAmount": 0,
                "deposit": 0,
                "bonus": 100,
                "referralChildDeposit": 0,
                "children": [
                    {
                        "id": "ddf86d60-a607-4a4e-a7f9-d96013ee7070",
                        "name": "Rick Rich",
                        "email": "[email protected]",
                        "role": "user",
                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZGRmODZkNjAtYTYwNy00YTRlLWE3ZjktZDk2MDEzZWU3MDcwIiwiaWF0IjoxNjg1NDQ0NDYzLCJleHAiOjMzMjExNDg2ODYzfQ.LUtlnL-j5KVjTgLCkzV47Txt9nyxpt108CBDgEMYdKY",
                        "walletAmount": 0,
                        "deposit": 0,
                        "bonus": 100,
                        "referralChildDeposit": 0,
                        "children": [
                            {
                                "id": "25de2e98-eb2d-41f4-b225-3069f942b284",
                                "name": "Rick Rich",
                                "email": "[email protected]",
                                "role": "user",
                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMjVkZTJlOTgtZWIyZC00MWY0LWIyMjUtMzA2OWY5NDJiMjg0IiwiaWF0IjoxNjg1NDQ0NTA4LCJleHAiOjMzMjExNDg2OTA4fQ.EfPLd4A1loiC36UVAPeF4zjhyAqUsXJxxahpQsvxS3A",
                                "walletAmount": 0,
                                "deposit": 0,
                                "bonus": 100,
                                "referralChildDeposit": 0,
                                "children": [
                                    {
                                        "id": "376b202e-d44f-4402-9560-8498c855d05e",
                                        "name": "Rick Rich",
                                        "email": "[email protected]",
                                        "role": "user",
                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMzc2YjIwMmUtZDQ0Zi00NDAyLTk1NjAtODQ5OGM4NTVkMDVlIiwiaWF0IjoxNjg1NDQ0NTcyLCJleHAiOjMzMjExNDg2OTcyfQ.b3AvdAo9vuIzk0kCU2mPts8W5mfLMba78LF9SrW7en4",
                                        "walletAmount": 0,
                                        "deposit": 0,
                                        "bonus": 100,
                                        "referralChildDeposit": 0,
                                        "children": [
                                            {
                                                "id": "042681be-f521-40af-a1dd-a1591700f8e0",
                                                "name": "Rick Rich",
                                                "email": "[email protected]",
                                                "role": "user",
                                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMDQyNjgxYmUtZjUyMS00MGFmLWExZGQtYTE1OTE3MDBmOGUwIiwiaWF0IjoxNjg1NDQ0NjY4LCJleHAiOjMzMjExNDg3MDY4fQ.OTQ71d_mGw4wp4rQIStZPG0lKsZz0-NxgZnxjQ4cDgY",
                                                "walletAmount": 0,
                                                "deposit": 0,
                                                "bonus": 100,
                                                "referralChildDeposit": 0,
                                                "children": []
                                            },
                                            {
                                                "id": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                "name": "Rick Rich",
                                                "email": "[email protected]",
                                                "role": "user",
                                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOTk3ZTAyYTctMWJiNy00NzNmLThmZjgtNWViNzZkOGQzMDliIiwiaWF0IjoxNjg1NDQ0NjcyLCJleHAiOjMzMjExNDg3MDcyfQ.zD4TxMGTxtwQozzXmkUNHIkYvqgWWONkLW-mP1D446k",
                                                "walletAmount": 0,
                                                "deposit": 0,
                                                "bonus": 100,
                                                "referralChildDeposit": 0,
                                                "children": [
                                                    {
                                                        "id": "4195f2b0-2603-414d-96f5-2099e4fcfdb1",
                                                        "email": "[email protected]",
                                                        "isActivate": false,
                                                        "name": "Rick Rich",
                                                        "password": "$2b$10$BcwivMHVzAEe8/.vXxe.neZivaieoNNgX7iZ1i7bspXCXjbJZZMqi",
                                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                        "role": "user",
                                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiNDE5NWYyYjAtMjYwMy00MTRkLTk2ZjUtMjA5OWU0ZmNmZGIxIiwiaWF0IjoxNjg1NDQ0NzI0LCJleHAiOjMzMjExNDg3MTI0fQ.128GQLG0jU-2Tu-DwNJMD_EbUIUwtYcrTDXWfRbGdcY",
                                                        "referralChildDeposit": 0,
                                                        "referralBloggerChildDeposit": 0,
                                                        "walletAmount": 0,
                                                        "deposit": 0,
                                                        "bonus": 100,
                                                        "gemstone": 0,
                                                        "frozen": false,
                                                        "banned": false,
                                                        "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "createdAt": "2023-05-30T11:05:24.334Z",
                                                        "updatedAt": "2023-05-30T11:05:24.341Z"
                                                    },
                                                    {
                                                        "id": "f427ac9b-c7d5-49ef-a91f-722ac8d40d93",
                                                        "email": "[email protected]",
                                                        "isActivate": false,
                                                        "name": "Rick Rich",
                                                        "password": "$2b$10$UXnr0uGxTs9UW6wcckKiMuBvcOTsLwD1YtE.J/tS5PK8RoaTvO9Ce",
                                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                        "role": "user",
                                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZjQyN2FjOWItYzdkNS00OWVmLWE5MWYtNzIyYWM4ZDQwZDkzIiwiaWF0IjoxNjg1NDQ0NzI4LCJleHAiOjMzMjExNDg3MTI4fQ.F-2gl4pLIzp40VzehSP5X44aDLQE-SFEfb3GoJuc4MU",
                                                        "referralChildDeposit": 0,
                                                        "referralBloggerChildDeposit": 0,
                                                        "walletAmount": 0,
                                                        "deposit": 0,
                                                        "bonus": 100,
                                                        "gemstone": 0,
                                                        "frozen": false,
                                                        "banned": false,
                                                        "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "createdAt": "2023-05-30T11:05:28.471Z",
                                                        "updatedAt": "2023-05-30T11:05:28.478Z"
                                                    },
                                                    {
                                                        "id": "c063bea1-37d0-4173-805f-6ad1d7b628b8",
                                                        "email": "[email protected]",
                                                        "isActivate": false,
                                                        "name": "Rick Rich",
                                                        "password": "$2b$10$CT7xqTZQSE/G1KSiwDgx4.phGXBDreIKp29ZAK.EyRykn.nl3/oiq",
                                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                        "role": "user",
                                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYzA2M2JlYTEtMzdkMC00MTczLTgwNWYtNmFkMWQ3YjYyOGI4IiwiaWF0IjoxNjg1NDQ0NzMzLCJleHAiOjMzMjExNDg3MTMzfQ.8L1ds9YvJ3_SH1ENtq8epYOYT4Wj_jnIvaGYIvoSviM",
                                                        "referralChildDeposit": 0,
                                                        "referralBloggerChildDeposit": 0,
                                                        "walletAmount": 0,
                                                        "deposit": 0,
                                                        "bonus": 100,
                                                        "gemstone": 0,
                                                        "frozen": false,
                                                        "banned": false,
                                                        "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "createdAt": "2023-05-30T11:05:33.318Z",
                                                        "updatedAt": "2023-05-30T11:05:33.325Z"
                                                    },
                                                    {
                                                        "id": "169f5ad5-0405-42b6-9ce5-a50ca9d9e723",
                                                        "email": "[email protected]",
                                                        "isActivate": false,
                                                        "name": "Rick Rich",
                                                        "password": "$2b$10$CYPOO2BQydNdyt5/3eLXSeBam4nqlXcCMg4cKdDE6q.56OIewBw9y",
                                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                        "role": "user",
                                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMTY5ZjVhZDUtMDQwNS00MmI2LTljZTUtYTUwY2E5ZDllNzIzIiwiaWF0IjoxNjg1NDUzMjYzLCJleHAiOjMzMjExNDk1NjYzfQ.SxnCWoWRZ7uNho7Jf7orpG2Tcyv9Mu5gzxKcAZ8bpKc",
                                                        "referralChildDeposit": 0,
                                                        "referralBloggerChildDeposit": 0,
                                                        "walletAmount": 0,
                                                        "deposit": 0,
                                                        "bonus": 100,
                                                        "gemstone": 0,
                                                        "frozen": false,
                                                        "banned": false,
                                                        "parentId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "invitedUserId": "997e02a7-1bb7-473f-8ff8-5eb76d8d309b",
                                                        "createdAt": "2023-05-30T13:27:43.076Z",
                                                        "updatedAt": "2023-05-30T13:27:43.085Z"
                                                    }
                                                ]
                                            },
                                            {
                                                "id": "b969c5b8-2d01-4872-8063-8e9ca7474a13",
                                                "name": "Rick Rich",
                                                "email": "[email protected]",
                                                "role": "user",
                                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYjk2OWM1YjgtMmQwMS00ODcyLTgwNjMtOGU5Y2E3NDc0YTEzIiwiaWF0IjoxNjg1NDQ0Njc3LCJleHAiOjMzMjExNDg3MDc3fQ.IeAuHU8cNTehSPdXlPuFN-BeG_Fk6nwavnrCOa48lMM",
                                                "walletAmount": 0,
                                                "deposit": 0,
                                                "bonus": 100,
                                                "referralChildDeposit": 0,
                                                "children": []
                                            }
                                        ]
                                    },
                                    {
                                        "id": "d8252898-46b8-4cf4-a545-06e98e6dccf1",
                                        "name": "Rick Rich",
                                        "email": "[email protected]",
                                        "role": "user",
                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZDgyNTI4OTgtNDZiOC00Y2Y0LWE1NDUtMDZlOThlNmRjY2YxIiwiaWF0IjoxNjg1NDQ0NTc2LCJleHAiOjMzMjExNDg2OTc2fQ.Ox6sV1vfBemE33bwzFLiVmMiTOioBz_xxc968L81wEo",
                                        "walletAmount": 0,
                                        "deposit": 0,
                                        "bonus": 100,
                                        "referralChildDeposit": 0,
                                        "children": []
                                    },
                                    {
                                        "id": "0d6ae01b-ccda-4de3-83ca-d2f70dd0897c",
                                        "name": "Rick Rich",
                                        "email": "[email protected]",
                                        "role": "user",
                                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMGQ2YWUwMWItY2NkYS00ZGUzLTgzY2EtZDJmNzBkZDA4OTdjIiwiaWF0IjoxNjg1NDQ0NTgxLCJleHAiOjMzMjExNDg2OTgxfQ._M-lVXD7zxt5BRdhiIm2pCPJ4Yaikt-_eBGowlR8InU",
                                        "walletAmount": 0,
                                        "deposit": 0,
                                        "bonus": 100,
                                        "referralChildDeposit": 0,
                                        "children": []
                                    }
                                ]
                            },
                            {
                                "id": "0ee1ea8f-790f-4767-b280-c5cddfe9c630",
                                "name": "Rick Rich",
                                "email": "[email protected]",
                                "role": "user",
                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMGVlMWVhOGYtNzkwZi00NzY3LWIyODAtYzVjZGRmZTljNjMwIiwiaWF0IjoxNjg1NDQ0NTE1LCJleHAiOjMzMjExNDg2OTE1fQ.pis1sqBc6DZT1_INLEtKBdx_htymiLRiNE9obXzJ1B0",
                                "walletAmount": 0,
                                "deposit": 0,
                                "bonus": 100,
                                "referralChildDeposit": 0,
                                "children": []
                            },
                            {
                                "id": "eb530dbf-9026-4cb8-a3fa-d02133ced42d",
                                "name": "Rick Rich",
                                "email": "[email protected]",
                                "role": "user",
                                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZWI1MzBkYmYtOTAyNi00Y2I4LWEzZmEtZDAyMTMzY2VkNDJkIiwiaWF0IjoxNjg1NDQ0NTIwLCJleHAiOjMzMjExNDg2OTIwfQ.3FVnsBwylesvv8Lf9Uyb4cA1d1399JFie-6F__6-nr0",
                                "walletAmount": 0,
                                "deposit": 0,
                                "bonus": 100,
                                "referralChildDeposit": 0,
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": "d1022d3c-25af-461c-af91-cbc64147f92c",
                        "name": "Rick Rich",
                        "email": "[email protected]",
                        "role": "user",
                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZDEwMjJkM2MtMjVhZi00NjFjLWFmOTEtY2JjNjQxNDdmOTJjIiwiaWF0IjoxNjg1NDQ0NDczLCJleHAiOjMzMjExNDg2ODczfQ.49qPRgY8DW1M8za7WY51LepIzhtVG_863p7l4CJFLRg",
                        "walletAmount": 0,
                        "deposit": 0,
                        "bonus": 100,
                        "referralChildDeposit": 0,
                        "children": []
                    },
                    {
                        "id": "5927f160-e4ad-4b13-a47e-b1d4b0687897",
                        "name": "Rick Rich",
                        "email": "[email protected]",
                        "role": "user",
                        "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                        "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiNTkyN2YxNjAtZTRhZC00YjEzLWE0N2UtYjFkNGIwNjg3ODk3IiwiaWF0IjoxNjg1NDQ0NDc4LCJleHAiOjMzMjExNDg2ODc4fQ.7nf966SGK-umk4LgjF2AMvzcznRS1vg7akLjYcHAXb4",
                        "walletAmount": 0,
                        "deposit": 0,
                        "bonus": 100,
                        "referralChildDeposit": 0,
                        "children": []
                    }
                ]
            },
            {
                "id": "8dc79a93-3893-45ca-ba15-3bb3c1e7061d",
                "name": "Rick Rich",
                "email": "[email protected]",
                "role": "user",
                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOGRjNzlhOTMtMzg5My00NWNhLWJhMTUtM2JiM2MxZTcwNjFkIiwiaWF0IjoxNjg1NDQ0MzkwLCJleHAiOjMzMjExNDg2NzkwfQ.KH81VuIHXUqGrq68rB7b-lTrpfHU8MnsUMo96a83Rus",
                "walletAmount": 0,
                "deposit": 0,
                "bonus": 100,
                "referralChildDeposit": 0,
                "children": []
            },
            {
                "id": "9ded36ef-ac0f-42a4-b46f-58ca522d0a71",
                "name": "Rick Rich",
                "email": "[email protected]",
                "role": "user",
                "avatar": "https://res.cloudinary.com/dbcqdew9c/image/upload/v1681384130/b3yadfh6nugp77cpe0sn.png",
                "referralLink": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiOWRlZDM2ZWYtYWMwZi00MmE0LWI0NmYtNThjYTUyMmQwYTcxIiwiaWF0IjoxNjg1NDQ0Mzk2LCJleHAiOjMzMjExNDg2Nzk2fQ.ie03HNsGdWiDRe_m7EBNvp31ygqhVwfPGqtyvlOTc-c",
                "walletAmount": 0,
                "deposit": 0,
                "bonus": 100,
                "referralChildDeposit": 0,
                "children": []
            }
        ]
    }
    
    const count = (children, arr = [], level = 0) => {
        children.forEach(node => {
            arr[level] = (arr[level] || 0) + 1;
            if (node.children) {
                count(node.children, arr, level + 1);
            }
        })
    
        return arr;
    };
    
    const output = count(data.children);
    
    console.log(output);
    Login or Signup to reply.
  3. Its not Particularly elegant like the other answers but this is an easy way of understanding how it works:

    let firstLayer = children.length
    let secondLayer = 0
    let thirdLayer = 0
    let fourthLayer = 0
    let fifthLayer = 0
    let sixfthLayer = 0
    
    for (let i = 0; i < children.length; i++) {
      secondLayer += children[i].children.length
      for (let j = 0; j < children[i].children.length; j++) {
        thirdLayer += children[i].children[j].children.length
        for(let f = 0; f < children[i].children[j].children.length; f++) {
          fourthLayer += children[i].children[j].children[f].children.length
          for (let e = 0; e < children[i].children[j].children[f].children.length; e++) {
            fifthLayer += children[i].children[j].children[f].children[e].children.length
            for (let g = 0; g < children[i].children[j].children[f].children[e].children.length; g++) {
              sixfthLayer += children[i].children[j].children[f].children[e].children[g].children.length
            }
          }
        }
      }
    }
    const output = [firstLayer, secondLayer, thirdLayer, fourthLayer, fifthLayer, sixfthLayer]
    console.log(output)
    

    nested For loops loop through the children and add to the layers total only problem with this is its not scalable with more layers
    Hopefully this helps you understand.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search