How can I get a value from a nested object, using an array of keys?
// my sample object
var obj = {
type : "Purchase",
category : "Apartment",
categoryOptions : {
apartment : {
floors : {
type : "number",
value : null,
placeholder : "Total Floors"
},
},
},
}
var keysArray = ["value", "floors", "apartment", "categoryOptions"]
I tried to use array.reduceRight to achieve this but could not make it work.
here is what I've tried :
var roadToValue = keysArray.reduceRight(
function(previousValue, currentValue){
return previousValue + "[" + currentValue + "]" ;
}
);
// above function results in a single string like
// "categoryOptions[apartment][floors][value]"
// which off-course can't be used as object key
// and obj[roadToValue] results in 'undefined'
is there any way so I can get the proper key to pass to obj here?