You'd need to reference each property via bracket notation. So, object[path]
won't work (as @Crazy Train pointed out, your debugging should have shown you that your var path = ...
throws an error), but if you have an array of paths like let path = ['india', 'chennai', 'sholing']
then you can use them to access the deep property like object[path[0]][path[1]][path[2]]
.
You're probably much better off using a utility helper to do this for you, such as Lodash's toPath and get functions:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
// => 3
_.get(object, ['a', '0', 'b', 'c']);
// => 3
_.get(object, 'a.b.c', 'default');
// => 'default'
See other answers: