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

130
Visualizações
Global JSON Search

I’m creating a function to search information in a json structure, my function works well in the first level but I have problems to search in the second level. My funtion return all the object json as if the search had never been performed.

this is my code:

json:

let machine = [
    {
        "sku": "qweert-12",
        "type": "aaaa",
        "components":[
            {
                "unit": "mmmm",
                "sku": "qwer-12"
            },
            {
                "unit": "llll",
                "sku": "qwer-14"
            }
        ]
    },
    {
        "sku": "qmqert-12",
        "type": "bbbb",
        "components":[
            {
                "unit": "ssss",
                "sku": "qwlr-12"
            },
            {
                "unit": "jjjj",
                "sku": "qwuer-14"
            }
        ]
    },
]

function

const search= (inputUser, data, setReturninfo) => {
    const input = inputUser.target.value();

    const result = data.filter((data) => {

        let component= data.components;
        return Object.keys(component).some((key) => {
            return JSON.stringify((data[key])).toLocaleLowerCase().trim.includes(input);
        })
    });

    setReturninfo(result);
}

I appreciate any help.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

var searchResults = []; //this array will be filled with results that match the searched key string

function search(jsonObject, key) {
    for (let k in jsonObject) {
        let innerObject = jsonObject[k];
        
        if (k == key) {
            searchResults.push(innerObject);
        }
        
        //recursively search for arrays and objects deep inside:
        if (innerObject instanceof Array || typeof innerObject == "object") {
            search(innerObject, key);
        }
    }
}

search(machine, "sku");
//searchResults will be: [ "qweert-12", "qwer-12", "qwer-14", "qmqert-12", "qwlr-12", "qwuer-14" ]

Using a recursive function to search nested levels. Hope I got the function you where searching for.

about 4 years ago · Santiago Trujillo 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