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

233
Vistas
How to ready dynamic key value from object in js
const a = {"b":{"c":3}}
const z = "b.c"

I have the above values and now I want to get value 3(a.b.c) from a using the z variable. I have tried the below method but it is not working

  • a.z
  • ${a.z}
  • ${a[z]}
  • ...etc..
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do something like this

function rGetAttr(obj, dotPath){
  return dotPath.split('.').reduce((o,i)=> o[i], obj)
}

const obj = {a: {b: {c: 123}}}
console.log(rGetAttr(obj, "a.b.c"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it by using Lodash


const _ = require("lodash");

console.log(_.get(a, z));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do something like this:

const a = {
  "b": {
    "c": 3
  }
}

const keyName = "b";

// This works to access an objects elements by key name
console.log(a[keyName]);

// If you want to be able to have "b.c" then you need to handle the nesting something like this:
const getValue = (obj, prop) => {
    const parts = prop.split('.');
    let thisObj = obj;
    let part = 0;
    while (thisObj = thisObj?.[parts[part++]]) {
        if (part >= parts.length) break;
    }
    return thisObj;
}


const nestedKeyName = "b.c";
// Usage like this:
console.log(getValue(a, nestedKeyName));

about 4 years ago · Juan Pablo Isaza 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