最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

key as variable when accessing javascript dictionary - Stack Overflow

programmeradmin0浏览0评论

i am trying to store css values for transformations of multiple divs in a dictionary to access them when the div is clicked (the div id being the key of the dict).

my dictionary:

css_dict = {
    'keyA' : ['10px, 15px', '20px', '10px'],
    'keyB' : ['20px, 30px', '10px', '20px'],
    'keyA' : ['30px, 55px', '25px', '30px'],
}

i need to access it like this (variable instead of the key):

css_dict.var[0]

jsFiddle of what im trying to do: /

i cant figure out or find the right syntax for this to work...

i am trying to store css values for transformations of multiple divs in a dictionary to access them when the div is clicked (the div id being the key of the dict).

my dictionary:

css_dict = {
    'keyA' : ['10px, 15px', '20px', '10px'],
    'keyB' : ['20px, 30px', '10px', '20px'],
    'keyA' : ['30px, 55px', '25px', '30px'],
}

i need to access it like this (variable instead of the key):

css_dict.var[0]

jsFiddle of what im trying to do: http://jsfiddle.net/tKFka/25/

i cant figure out or find the right syntax for this to work...

Share Improve this question asked Jan 6, 2013 at 15:04 user1952949user1952949 571 gold badge1 silver badge3 bronze badges 2
  • Try css_dict[var][0]. jsfiddle.net/tKFka/28 – Rikonator Commented Jan 6, 2013 at 15:06
  • Is there any literal form? such as $ in var mykey="k", mydict={$mykey: 'value'} – Sohail Si Commented Nov 27, 2018 at 19:25
Add a comment  | 

1 Answer 1

Reset to default 15

Use [] instead of dot.

var var1 = 'keyA';
css_dict[var1][0];
发布评论

评论列表(0)

  1. 暂无评论