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

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

0

You can use Array.reduce() for this:

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

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 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!