v1
- by souljorje 11/23/202100
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const getPropByPath1 = (object, path, defaultVal) => {
  const _path = Array.isArray(path)
    ? path
    : path.split('.');

  if (!_path.length) {
    return object === undefined ? defaultVal : object
  }

  return getPropByPath1(object[_path.shift()], _path, defaultVal)
}

const getPropByPath2 = (obj, path, defaultValue) => {
  const travel = regexp =>
    String.prototype.split
      .call(path, regexp)
      .filter(Boolean)
      .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj);
  const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
  return result === undefined || result === obj ? defaultValue : result;
};


const getPropByPath3 = (object, path, defaultValue) => {
    const _path = Array.isArray(path)
    	? path
    	: path.split('.');
    if (object && _path.length) return getPropByPath3(object[_path.shift()], _path, defaultValue);
    return object === undefined ? defaultValue : object;
};


const obj = {
	mobile: {
            aside: true,
            isTitle: true,
        },
    };
    
const path = 'mobile.isTitle.foo.bar';
const def = 'foo'; 
delete caserun single casemove downdrag and drop case


ready



// https://gist.github.com/harish2704/d0ee530e6ee75bad6fd30c98e5ad9dab#gistcomment-2339206
// throws and error 
getPropByPath1(obj, path, def);
delete caserun single casemove upmove downdrag and drop case


ready



// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get
getPropByPath2(obj, path, def);
delete caserun single casemove updrag and drop case


ready



getPropByPath3(obj, path, def);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN