# 如何实现类似 lodash.get 函数

Issue

欢迎在 Issue 中交流与讨论: Issue 199 (opens new window)

Author

回答者: miaooow (opens new window)

function lodashGet(obj,exps){ if(typeof exps !== 'string') return obj if(typeof obj !== 'object') return obj let res = obj const arr = exps.split('.') for(let i=0;i<arr.length;i++){ const exp = arr[i] if(res[exp]){ res = res[exp] } else{ return undefined } } return res }

var obj = {test:{arr:[{name:1}]}}

lodashGet(obj,'test.arr.0.name')

Last Updated: 1/15/2021, 6:07:33 AM