Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

0

You can use Array.reduce() for this:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda