I've got a recursive function to go through an object and take what is needed using path string like jsonObj.object.item.value. The idea is to upgrade it somehow to support arrays.
const getValue = function(pathVar, obj) {
obj = obj || x;
// get very first node and the rest
let [node, restNodes] = pathVar.split(/\.(.+)/, 2)
// get interim object
ret = obj[node];
if(ret === undefined) return undefined;
// pass rest of the nodes further with interim object
if(restNodes) return getValue(restNodes, ret);
return ret;
};
Right now on each iteration simple regexp splits path string like jsonObj.object.item.value into jsonObj and object.item.value
the idea is to add array support here, so I can do transformations like
car.engine.something => car and engine.something
wheels[2].material.name => wheels and [2].material.name
[2].material.name => 2 and material.name
car.wheels[4] => car and wheels[4]
Any ideas how to do it?
_.getmethod that does exactly that, lodash.com/docs/4.17.11#get