const people = {
base: {
name: 'zhang san',
age: 25,
clothes: [
{ name: 'jack', color: 'red' },
{ name: 'suit', color: 'black' }
]
},
children: [
{ name: 'son1', gender: 'male', age: 3 },
{ name: 'girl1', gender: 'female', age: 1 }
]
}
function getObjectByPath (obj, path) {
// return result
}
console.log(getObjectByPath(people, 'base.age')) // 25
console.log(getObjectByPath(people, 'base.clothes[0].name')) // jack
console.log(getObjectByPath(people, 'children[0].name')) // son1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…