尝试了一下: function getValueByPath(a, str) { const keys = str.split('.'); try { const result = keys.reduce((acc, current) => { if (/.+\[\d+\]/g.test(current)) { const matchs = (/^(.+)\[(\d+)\]/g).exec(current); const arr = acc[matchs[1]]; const index = matchs[2]; return arr[index]; } const key = current; return acc[key]; }, a); return result; } catch (error) { return undefined } } const x = { a: { b: [{ c: 1 }] } }; const y = []; console.log(getValueByPath(x, 'a.b[0].c')); console.log(getValueByPath(y, 'a.b[0].c')); 顺便问下,你什么时候投的以及什么时候笔试的,为什么我还没收到面试通知。
点赞 2

相关推荐

牛客热帖

牛客网
牛客企业服务