手写算法-lodash get

lodash get

function get(source, path, defaultValue = undefined){
  const paths = path.replace(/\[(\d+)\]/g, '$1').split('.')
  let result = source;
  for(let p of paths){
    result = Object(result)[p]
    if(result === undefined){
     return defaultValue
    }
  }
 return result
}
posted @ 2022-12-27 12:09  yiyunh  阅读(120)  评论(0)    收藏  举报