I'm getting a big, nested JSON response from an online API. I require only 3 values from it, but they are quite nested. I do know the exact path to each of those fields. For example, given:
obj = {
    "result": {
        "data": {
            "point1": "x",
            "value": 2
        },
        "foo": {
            "bar": 7
        }
    }
}
And the path let p = 'result.data.value' I'd like to be able to do let x = getElement(obj, p); and have x be 2.
I wrote a simple function (removed all the error checking to keep clean):
const getJSONElement = (data, path) => {
  for(let p of path.split('.')) {
    data = data[p];
  }
  return data;
};
It works, but I feel like I'm missing something, or not being efficient. Is there there's a better way to get to the element (perhaps using Array.reduce)?
 
    