📜  _.get 替代方案 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:14.353000             🧑  作者: Mango

代码示例1
//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