Answers for "_.get alternative"

0

_.get alternative

//This Is my alternative to (lodash) _.get() method 

const get = (obj, path, def) => (() => typeof path === 'string' ? path.replace(/\[(\d+)]/g,'.$1') : path.join('.'))()
  .split('.')
  .filter(Boolean)
  .every(step => ((obj = obj[step]) !== undefined)) ? obj : def



//Usages Example
var prop = get(data, '[0].items[0].property', 'backupValue');

//returns found value of path
Posted by: Guest on January-04-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language