0

I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:

var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);

I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?

EDIT: date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object.. uppon reading the mod suggested post and implementing what it contained, i ended up with another error

Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
user2620255
  • 137
  • 1
  • 6
  • You are right, it has nothing to do with JSON. What you are having is an object and you are creating it using an *object literal*. – Felix Kling Apr 15 '15 at 02:10
  • ignore that last comment! :O totaly didn't see the reference above, thanks ill be sure to check that out, didn't know about object literal! ^_^; – user2620255 Apr 15 '15 at 02:14
  • didn't help sadly, just added a new problem – user2620255 Apr 15 '15 at 03:44
  • I think the point you might be overlooking is that the variable `timestamp` of line 2 is completely different from the object literal key `timestamp` on line 3. If you want the code to behave like you expect, try: `Que[date] = {}` and then `Que[date][timestamp] = {test:'test'}` and you should get the structure you expect. – Andrew Lavers Apr 15 '15 at 03:53
  • If you are not using a transpiler that converts ES6 to ES5 code then you cannot use the solution in your edit. Use the other one instead. – Felix Kling Apr 15 '15 at 04:15
  • ok downloaded io.js but it didn't help at all. i still get the same error when i try and do it the final way mentioned. {[var]:"vale"}; – user2620255 Apr 15 '15 at 06:19
  • @AndrewLavers what i wanted was for the value of "time stamp" to be the name for the key. not provide the data for they key. i.e. if timestamp = 100 i want the name of the key to be "100" – user2620255 Apr 15 '15 at 22:24

1 Answers1

0

I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using

Que[date]["timesheet"]

To access the object inside. The key that you're looking up with the [] notation should be a string.