admin管理员组文章数量:1417045
This is an odd thing to ask I am aware, but I am very much a newbie and can't seem to wrap my head around this. I have a Javascript object sent to me via firebase that looks like this:
var blob = {
matt@email,: { //notice the ma because periods are illegal in keys
email: "[email protected]" //actual email with period
name: "Matt Sanford"
pic: ".jpg"
provider: "google"
uid: "0000000000000000"
}
}
}
I am trying to access the inner most tree via the console like so: console.log(blob.matt@email,) //throws an error because of an invalid token
even though it should return the object with email
, name
, etc.
However when I tried the same structure like so:
var blob = {foo: {bar: true} }
console.log(blob.foo) //output '{bar: true}'
There are two things I am wondering, is having the initial key with the modified email illegal because of the mas or is there not a way to read such a key in javascript? Remendations are appreciated because I am just learning as I go along here.
Update
How would I go about accessing the keys dynamically? Clearly it would be impossible to input each key dynamically. How would I read it without knowing what exactly the key name is?
This is an odd thing to ask I am aware, but I am very much a newbie and can't seem to wrap my head around this. I have a Javascript object sent to me via firebase that looks like this:
var blob = {
matt@email,: { //notice the ma because periods are illegal in keys
email: "[email protected]" //actual email with period
name: "Matt Sanford"
pic: "https://lh3.googleusercontent./-LeQrq-_KjJE/AAAAAAAAAAI/AAAAAAAAAoI/4l6r2HNdock/photo.jpg"
provider: "google"
uid: "0000000000000000"
}
}
}
I am trying to access the inner most tree via the console like so: console.log(blob.matt@email,) //throws an error because of an invalid token
even though it should return the object with email
, name
, etc.
However when I tried the same structure like so:
var blob = {foo: {bar: true} }
console.log(blob.foo) //output '{bar: true}'
There are two things I am wondering, is having the initial key with the modified email illegal because of the mas or is there not a way to read such a key in javascript? Remendations are appreciated because I am just learning as I go along here.
Update
How would I go about accessing the keys dynamically? Clearly it would be impossible to input each key dynamically. How would I read it without knowing what exactly the key name is?
Share Improve this question edited Jan 12, 2016 at 17:53 sanch asked Jan 11, 2016 at 5:50 sanchsanch 7161 gold badge7 silver badges22 bronze badges 2- 1 Two things, 1. When the key of an object contain special symbol, use quotes 2 Use ma as separator between two elements of object – Tushar Commented Jan 11, 2016 at 5:52
-
4
console.log( blob['matt@email,'] )
– Paul Commented Jan 11, 2016 at 5:53
1 Answer
Reset to default 9What you posted is not a json object, it's a javascript object. JSON would have all its keys quoted.
Comma's are definitely allowed, but you cannot use the standard obj.property
syntax like this:
console.log(blob.matt@email,)
You must do:
console.log(blob['matt@email,']);
本文标签: angularjsWhy can39t commas be in Javascript object keysStack Overflow
版权声明:本文标题:angularjs - Why can't commas be in Javascript object keys? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257080a2650165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论