JSFiddle - React, Tailwind, and code Playground

by evgkch

JavaScript

function get(targetObj, path, defaultValue = undefined){
	const splittedPath = path.split('.');
  return _get(targetObj, splittedPath);
  function _get(targetObj, path){
  	if (path.length != 0)
    {
    	if (typeof(targetObj) == 'object' && (path[0] in targetObj))
      	return _get(targetObj[path[0]], path.slice(1));
      else
      	return defaultValue;
    }
    else
    	return targetObj;
  }
}

console.log(get({ a: { b: 1 }}, 'a.b.c.d'))