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

189
Visualizações
using the value from one JSON as the key in another

I have 2 JSONs. One contains data like:

const data = {
"dataElements": {
    "someField": "someData1",
    "someOtherField": "someOtherData"
},
"anotherDataElement": {
    "yetMoreFields": "evenMoreData",
    "stillMore": "dater"
}}

The other contains something like:

const lookUp = {
"lookUpValues": {
    "valueOne": "data.dataElements.someField",
    "someOtherField": "someOtherData"
}}

What I need to do is take the value of lookup.lookUpValues.valueOne (which would resolve to "data.dataElements.someField") and get that value from the data object ("someData1")

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

0

JavaScript or Node.js does not provide any function to achieve your goal but you can write such a function yourself in several lines of code:

function getValue(object, path) {
  // split the path in pieces separated by '.'
  const pieces = path.split('.');

  // walk the path, for each piece get the property with the same name from the current object
  return pieces.reduce(
    // result = the current object being walked
    // piece = the name of the current path piece
    // return the `piece` property of the `result` object (or `undefined` if it is not an object)
    (result, piece) => result instanceof Object ? result[piece] : undefined,
    // start walking with the provided object
    object
  );
}

Then call it like:

const value = getValue(data, 'dataElements.someField');

Check it online.

over 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