0

I have a nested object like this:

let obj = {
a : { 
  b : {
    c : 123
    }
  },
d:{
 e : 321
 }
}

what I'm trying to do is to create a function that accepts arguments as keys and changes the object values and save the whole object in a file (using JSON stringify); sth like this:

let var1 = "a" , var2 = "b" , var3 = "c" , var4 = "d" , var5 = "e";

obj.var1.var2.var3 = 0; // of course it's not right, just to show the main concept

obj.var4.var5 = 1;

let dataStr = JSON.stringify(obj);

fs.writeFile("path" , dataStr);

I have tried many approaches but none of them worked properly. my object is very large and nested. the "obj" shown above is just a sample. would anyone pls suggest the best approach for this

**

update:

** I did not found any useful solution for my problem yet. I found this:

function mod(path){
let res = obj;
path = path.split(".");
for(i = 0; i < path.length; i++) res = res[path[i]];
return res
}

which was very helpful but not for me because it only returns the value. what I want to do is change that value and save the whole object in a file.

0 Answers0