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

140
Visualizações
How do i get the value of an object in a object string

Hello there im kinda new to this so what i want to do is

string = {
    "string2": {
        "value": ""
    }
}

var path = ["string2","value"]

can i somehow get the value with the path i tried alot of things but nothing really worked

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

0

You can use Array.reduce() for this:

const value = path.reduce((accum, key) => accum[key], string)
about 4 years ago · Santiago Trujillo Relatório

0

There are several approaches. First, you can use a loop to traverse the path step by step as follows:

const obj = { "string2": { "value": "Message" } };
const path = [ "string2", "value" ];

let output = obj;
path.forEach(key => {
    output = output[key];
});

console.log( output );

RECURSION

const obj = { "string2": { "value": "Message" } };
const path = [ "string2", "value" ];

const trav = (o,p,i) => (i < p.length - 1) ? trav(o[p[i]],p,i+1) : o[p[i]];

console.log( trav(obj,path,0) );

new Function

const obj = { "string2": { "value": "Message" } };
const path = [ "string2", "value" ];

const output = (new Function(`return (obj.${path.join('.')})`))();

console.log( output );

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