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
463 views
in Technique[技术] by (71.8m points)

javascript - 将点表示法的JavaScript字符串转换为对象引用(Convert JavaScript string in dot notation into an object reference)

Given a JS object

(给定一个JS对象)

var obj = { a: { b: '1', c: '2' } }

and a string

(和一个字符串)

"a.b"

how can I convert the string to dot notation so I can go

(如何将字符串转换为点表示法,以便我可以)

var val = obj.a.b

If the string was just 'a' , I could use obj[a] .

(如果字符串只是'a' ,我可以使用obj[a] 。)

But this is more complex.

(但这更复杂。)

I imagine there is some straightforward method but it escapes at present.

(我想有一种简单的方法,但是目前可以逃脱。)

  ask by nevf translate from so

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

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...