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
check if item exists in json file then get it if exists. And if not complete executing the code

I get a json file from outer link by ajax call. i want to check element is exists in this json file or not and get it if exists. because it changes every time and items increases or decreases. If I get item and this item not existing, I get error and the code not complete. i want to check this item json.children[1].children and json.children[2].children ,..... if exists.

{
    "num": 1,
    "name": "aa",
    "properites": [
        {
            "name": "prop1",
            "value": "value1"
        }
    ],
    "children": [
       
        {
            "num2": 1,
            "name2": "name2",
            "properites": [
                {
                    "name": "name",
                    "value": "value"
                }
            ],
            "children": []
        },
       
        {
            "name": 1,
            "num": "1",
            "attributes": [
                {
                    "name": "name",
                    "value": "value"
                }
            ],
            "children": []
        .......................... big json file

I have tried to get these Items and set in local storage to use in table by this:

localStorage.setItem("row1-item1", json.children[1].children);
    localStorage.setItem("row1-item2", json.children[2].children);
    localStorage.setItem("row1-item3", json.children[3].children);
    localStorage.setItem("row1-item4", json.children[4].children);
    localStorage.setItem("row2-item1", json.name[1].children);
    localStorage.setItem("row2-item2", json.name[2].children);
    localStorage.setItem("row2-item3", json.name[3].children);
    localStorage.setItem("row2-item4", json.name[4].children);

the problem here is if json.children[4].children not existing the code not complete the next line and jump out of ajax call even if json.name[1].children is exists. I tried try/catch but not solved it

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

0

Try to use map

json.children.map((children,index)=>{
    localStorage.setItem('row${index}-item${index}`, children);
}

Or you can use forEach or for loop instead of map.

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