Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
How to find the length of particular type within object using javascript?

i have object like below,

Example 1

input = {
    item_type: {
        id: ‘1’,
        name: ‘name1’,
    },
    children: [
        {
            type_1:  {
                id: ‘12’,
            },
            type: 'item1-type',
        },
        {
            children: [
                {
                    id: '1',
                    type: 'item2',
                },
                {
                    id: '2',
                    type:'item2',
                },
                {
                    id:'3',
                    type: 'item2',
                },
            ] 
            
            type: 'item2-type',
        },
        {
            children: [
                {
                    id: '4',
                    type: 'item2',
                },
                {
                    id: '5',
                    type:'item2',
                },
                {
                    id:'6',
                    type: 'item2',
                },
            ] 
            
            type: 'item2-type',
        },
    ]
}

now i want to find the count of "item2" type within children array within children array again.

note that the outer children array can be empty array and the children array within children array may not be present. so the input can be of types like below

input = {
    item_type: {
        id: ‘1’,
        name: ‘name1’,
    },
    children: [] //empty children array
}


input = {
    item_type: {
        id: ‘1’,
        name: ‘name1’,
    },
    children: 
        [ //no children array within
            {
                type_1:  {
                    id: ‘12’,
                },
                type: “item1-type”,
            },
        ]
    }

how can i find the count of type: "item2" within children array considering example1 input.

so the expected count is 6.

could someone help me with this. thanks. new to programming.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const findAllChildrenOfType = (obj, type) => {
    let count = 0;
    if (obj.type === type) count++;
    if (obj.children) {
        obj.children.forEach(child => {
            const childCount = findAllChildrenOfType(child, type);
            count += childCount;
        })
    }
    return count;
}

console.log(findAllChildrenOfType(input, "item2"))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda