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

106
Visualizações
javascript for loop with recursion and return value example

I am trying to convert json object into array of path string but not getting expected result. Don't know might be other way to achieve the same but tried using for loop and lodash isObject and isEmpty for validate object like below.

Input Json:

    const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

javascript code:

var pathArray = this.convertJsonToArrayOfPathString(json);
    console.log(pathArray);
convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      path = path ? path + '.' + key : key;

      if (_.isObject(json[key]) && !_.isEmpty(json[key])) {
        this.convertJsonToArrayOfPathString(json[key], pathArray, path);
      }
      else {
        pathArray.push(path)
        path = ''
      }
    }
    return pathArray;
  }

expected output:

[
      "info.account",
      "info2.address.mobile.phone",
      "info2.contact.first"
    ]

could you please any help. thanks.

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

0

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
};
var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);
function convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      const newPath = path ? path + '.' + key : key;
      const value = json[key];
      if (typeof value === 'object' &&
            !Array.isArray(value) &&
            value !== null && Object.keys(value).length>0) {
        convertJsonToArrayOfPathString(json[key], pathArray, newPath);
      }
      else {
        pathArray.push(newPath);
      }
    }
    return pathArray;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);


function convertJsonToArrayOfPathString(json, pathArray = [], path = '') 
{
    Object.keys(json).forEach( key =>
    {
      const p = path ? path + '.' + key : key;
      var test = !json[key] || Object.keys( json[key]).length < 1 || Array.isArray( json[key]) ||
                typeof  json[key] !== 'object';
      return  test ? 
              pathArray.push(p) :
              this.convertJsonToArrayOfPathString(json[key], pathArray, p);
    });
    
    return pathArray;
}

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