I need help with getting property of object with String in JS.
I have object
elements = {
element : {
date: {
day: \'Mo
You can do something like this
var elements = {
element: {
date: {
day: 'Monday'
},
example: {
abc: 'hii'
}
}
};
function getObjectByStringIdentifier(stringId) {
stringId = stringId.split('.');
// split string using `.`
var res = elements;
// define res as object
for (var i = 0; i < stringId.length; i++)
// iterate over array
res = res[stringId[i]]
// update res as inner object value
return res;
// return result
}
console.log(getObjectByStringIdentifier("element.date.day"));
console.log(getObjectByStringIdentifier("element.example.abc"));