0

I am getting the node path as "a.b.c" from the server and have to read its value of it from the object. Can you guide me for the way to do it? I can do the recursive map function, although if there is another simple way to do it.

let js = {
      a: {
        b:{
          c: 1
        },
        e: 5
      },
      d: 2
    }
    let st = "a.b.c";//GETING THIS FROM SERVER
    let st2 = ["a","b","c"];
    let st3 = ["[a][b][c]"];
    console.log(js[st]);
    console.log(js[st2]);
    console.log(js[st3[0]]);

Currently i am doing it like this,

const readJS = (a,js)=>{
    let aa = a.split('.');
  let val = js;
  for(let i=0; i < aa.length; i++){
    console.log(val);
    val = val[aa[i]];
  }
  return val;
}
console.log(readJS(st,js));

0 Answers0