admin管理员组文章数量:1417671
There is a syntax error in the following code:
<!DOCTYPE html>
<html>
<body>
Hello World!
<script type="text/javascript">
var obj = {'a:b': '1'};
alert(obj.a:b); // syntax error
</script>
</body>
</html>
So how to handle JavaScript objects with colons in key names?
I have to do this because I need to handle a feed in jsonp format from a remote server which I do not have control over, and there are colons in the key names of the returned jsonp (because the jsonp is converted from XML with namespaces in tags).
There is a syntax error in the following code:
<!DOCTYPE html>
<html>
<body>
Hello World!
<script type="text/javascript">
var obj = {'a:b': '1'};
alert(obj.a:b); // syntax error
</script>
</body>
</html>
So how to handle JavaScript objects with colons in key names?
I have to do this because I need to handle a feed in jsonp format from a remote server which I do not have control over, and there are colons in the key names of the returned jsonp (because the jsonp is converted from XML with namespaces in tags).
Share Improve this question edited Jun 5, 2010 at 19:34 Thanatos 44.4k17 gold badges96 silver badges152 bronze badges asked Jun 5, 2010 at 19:07 EthanEthan 18.9k16 gold badges53 silver badges74 bronze badges1 Answer
Reset to default 9Access them with:
obj['a:b']
The brackets are synonymous with .
except they accept strings (including variables!)
So obj.x
== obj['x']
, and if you had a variable foo = 'x'
then obj[foo]
would also be equal.
Don't let the syntax fool you, though. It may look like syntax for array access, but it is actually another way to access properties from objects.
本文标签: how to handle JavaScript objects with colons in key namesStack Overflow
版权声明:本文标题:how to handle JavaScript objects with colons in key names? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745276144a2651198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论