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

150
Visualizações
Find element in array of x depth Javascript

Is it possible to use the find() method within an array of depth x?

For example, suppose I have the following array of objects, call it test:

[
    {
        "id": "1",
        "title": "First",
    },
    {
        "id": "2",
        "title": "Second",
        "movies": [
            {
                "id": "3",
                "title": "Happy Gilmore",
                "Actors": [
                    {
                        "id": "4",
                        "title": "John Doe",
                    },
                    {
                        "id": "5",
                        "title": "Jane Doe",
                    },
                ],
                "Producers": [
                    {
                        "id": "6",
                        "title": "Max Smith",
                    },
                    {
                        "id": "7",
                        "title": "Richard Rocky",
                    },
                ],
            },
            {
                "id": "10",
                "title": "Billy Madison",
                "Actors": [
                    {
                        "id": "40",
                        "title": "John Smith",
                    },
                    {
                        "id": "50",
                        "title": "Alex Doe",
                    },
                ],
                "Producers": [
                    {
                        "id": "60",
                        "title": "Bob Smith",
                    },
                    {
                        "id": "70",
                        "title": "Polly Rocky",
                    },
                ],
            }
        ]
    }
]

Suppose I am looking for the "2" id. I can use the find() method to search the first level of the array and return the desired object by doing test.find(element => element.id === "2").

However, suppose I am now looking for the occurrence where the id is 4. As you can see from the above JSON, that element is within a sub array within test. Is there a way therefore where I can still search through test to find the element where id=4?

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

0

find cannot do this, but you can use it in a recursive approach:

function findDeep(arr, predicate) {
    let res = arr.find(predicate);
    if (res !== undefined) return res;
    for (let obj of arr) {
        for (let value of Object.values(Object(obj)).filter(Array.isArray)) {
            res = findDeep(value, predicate);
            if (res !== undefined) return res;
        }
    }
}

let test = [{"id": "1","title": "First",},{"id": "2","title": "Second","movies": [{"id": "3","title": "Happy Gilmore","Actors": [{"id": "4","title": "John Doe",},{"id": "5","title": "Jane Doe",},],"Producers": [{"id": "6","title": "Max Smith",},{"id": "7","title": "Richard Rocky",},],},{"id": "10","title": "Billy Madison","Actors": [{"id": "40","title": "John Smith",},{"id": "50","title": "Alex Doe",},],"Producers": [{"id": "60","title": "Bob Smith",},{"id": "70","title": "Polly Rocky",},],}]}];

let res = findDeep(test, obj => obj.id == "4");

console.log(res);

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