Get Path Index of Object

by Nalin Sajwan

JavaScript

// Return value of object as per keys array.
function multiIndex(obj,is) {
  return is.length ? this.multiIndex(obj[is[0]],is.slice(1)) : obj
}

// Convert (.) dot notation text to string
function pathIndex(obj, is) {
  return this.multiIndex(obj,is.split('.'))
}

function getObjPath (obj, path) {
	return path.split('.').reduce((o,i) => o[i], obj);
}
 
 var myObj = {name: 123123, event: {type: "assad", category: "qeqweq", soruce: "123131"}}
 
 console.log(pathIndex(myObj, "event.soruce"));
 
 console.log(getObjPath(myObj, "event.soruce"));