I have a function like this:
const jsonObject = {a: {b: 'c'}};
const x = 'a.b';
const properties = x.split('.');
const item = properties.reduce((obj, prop) => obj && obj[prop], jsonObject);
console.log(item); // prints 'c;
This function, dynamically traverses the jsonObject and prints the value.
This works fine, but this style of declaration doesn't support my environment. So I wan trying to convert this to function style declaration something like this:
const item = properties.reduce(function(obj, prop){
if(obj && obj[prop]) return obj[prop];
});
But this doesn't seems to be working. Its printing (item) undefined.
if
statement. – ryeballar