Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
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 answers
Answer question

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 Report

0

You can do it by using Lodash


const _ = require("lodash");

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!