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

90
Views
Getting object in object value

There is a function that returns a value for the key I specified.

function l(key){
    let selectedLang = window.localStorage.getItem('language') || lang.default;
    return lang[selectedLang][key];
}
const en = {
    sidebar : {
        "a" : "b",
    }
}

l('sidebar') // function gives an object, its okey

But want to using like this, how can i do that.

l('sidebar.a') // Now its undifined
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to dig in recursively, here's one possible solution (also a recursive function, although that's not necessary):

const obj = {foo: "Foo", bar: {baz: "Bar Baz"}}

const access = (object, path) => {
    const pathArray = Array.isArray(path) ? path : path.split(".");
    const lastIndex = pathArray.length - 1;
    return lastIndex > 0
        ? access(object, pathArray.slice(0, lastIndex))[pathArray[lastIndex]]
        : object[pathArray[0]];
}

console.log(access(obj, ["foo"]));
console.log(access(obj, ["bar", "baz"]));
console.log(access(obj, "bar.baz"));

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!