angularjs - Why can't commas be in Javascript object keys? - Stack Overflow

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

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
Add a ment  | 

1 Answer 1

Reset to default 9

What 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,']);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257081a4619020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信