0

I am a javascript noob. What I would like to do is take the name of my selector id and use it as the name for another array. I get all types of errors when I try to code this :(

var this.id = {};

or

var string(this.id) = {};

gives back some nasty errors :(

The this refers to my current selector and is the input I am using. this.id is giving me the proper string for the input ID. So that works, at least according to console.log()

This is what I am trying to do...

myObject = 
{"ID of selector" = {variable A = 123, variable B = 493, variable C = 293},
    "ID of selector" = {variable A = 493, variable B = 492, variable C = 342}    
}

Thanks for all help!

chrisdottel
  • 1,053
  • 1
  • 12
  • 21
  • methinks you want [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) from ECMAScript 6, of cource if you want `this.id` will be object, if `this.id` is `string` then simple object enought – Grundy Sep 02 '14 at 17:57
  • You cannot do that with `var` variables. Use an object, like the `myObject = {}` in your example, for dynamically keyed data stores. – Bergi Sep 02 '14 at 18:46

1 Answers1

0

You can achieve what you intent using the array/dictionary notation like:

var myObject = {};   
myObject["ID of selector"] = {"foo" : 1};

However you cannot access "ID of selector" like a property of the object but only as dictionary like:

myObject["ID of selector"].foo //<- outpus 1 using sample above
Dalorzo
  • 19,834
  • 7
  • 55
  • 102