JavaScript解析字符串和对象的引用 [英] JavaScript resolve reference from string and object
本文介绍了JavaScript解析字符串和对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用对象本身获取对象的引用
is it possible to get a reference to an object with the object itself
obj
obj
和字符串形式的属性
'address.town.street'
'address.town.street'
以便最终结算
obj.address.town.street
我可以像eval()函数一样将smth像immage一样。
i could immageine smth like the eval() function.
推荐答案
尝试
function getValue(obj, path) {
return path.split(".").reduce(function(obj, name){ return obj[name]}, obj);
}
这篇关于JavaScript解析字符串和对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文