Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

如何通过给定的对象访问路径,获取javascript对象的某个属性的值?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Lodash get方法


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...