If I have an Object like this:
let obj = {
a:{
b:{
c:{
d:{
e:'nonono'
}
}
}
}
}
and I know the structure of the Object like that:
now I want to change the innermost layer of the Object, it is the "e" property. I want to assign another value to "e". I don't want like these ways below:
obj.a.b.c.d.e = 'another value';
var str1 = 'a.b.c.d.e'; obj[str1[0]][str[1]][str[2]][str[3]][str[4]];
var str1 = 'obj.a.b.c.d.e'; var str = str1 + "='another value'"; eval(str);
above these, I can change the property 'e' of the Object's Value, but I think it`s not grace to express what I mean.
If I have the Array like that:
var arr= [a,b,c,d,e]
, I want to recursion a function to find the innermost layer Of the Object, but I try, if I reach the innermost layer of the Object, I lose the quote of the Object..... So I can't change the Object's Value that I want.
I think I run these code, if you can help me to run.
let obj = {
a: {
b: {
c: {
d: {
e: 'nonono'
}
}
}
}
}
let arr = ['a', 'b', 'c', 'd', 'e'];
let funKeepCite = (obj, index) => {
if (obj[arr[index]]) {
funKeepCite(obj[arr[index]], index + 1);
} else {
obj = 'test'
}
}
funKeepCite(obj, 0)
console.log('the result', obj)
I can't change the value, I think I lose the quote of the Object, but the Answer of my question is use for .. in
, and it can keep the quote of the Object, I am confused of these.