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

javascript - How to use a variable value for the key of another object? - Stack Overflow

programmeradmin0浏览0评论

I have something like :

var myMenu= [
  {
    'My English Title':function(menuItem,menu) 
                       { ... }
  }];

Now, I want the replace 'My English Title' by an other JSON structure value LOC.MENU_TITLE.

I have try:

var myMenu= [
  {
    LOC.MENU_TITLE:function(menuItem,menu) 
                       { ... }
  }];

But it doesn't work. Any one can give me an hint of how to get JSON value inside a JSON variable?

Edit: This is to use the Context Menu of Jquery but I got a problem with all solutions below and it's when it's time to pass the String for the Separator. It doesn't show the separator because it pass the string into an object instead of just the string.

Example:

var menu1 = [
  {'Option 1':function(menuItem,menu) { alert("You clicked Option 1!"); } },
  $.contextMenu.separator,
  {'Option 2':function(menuItem,menu) { alert("You clicked Option 2!"); } }
];

I have something like :

var myMenu= [
  {
    'My English Title':function(menuItem,menu) 
                       { ... }
  }];

Now, I want the replace 'My English Title' by an other JSON structure value LOC.MENU_TITLE.

I have try:

var myMenu= [
  {
    LOC.MENU_TITLE:function(menuItem,menu) 
                       { ... }
  }];

But it doesn't work. Any one can give me an hint of how to get JSON value inside a JSON variable?

Edit: This is to use the Context Menu of Jquery but I got a problem with all solutions below and it's when it's time to pass the String for the Separator. It doesn't show the separator because it pass the string into an object instead of just the string.

Example:

var menu1 = [
  {'Option 1':function(menuItem,menu) { alert("You clicked Option 1!"); } },
  $.contextMenu.separator,
  {'Option 2':function(menuItem,menu) { alert("You clicked Option 2!"); } }
];
Share Improve this question edited Feb 8, 2010 at 20:56 Patrick Desjardins asked Feb 8, 2010 at 20:11 Patrick DesjardinsPatrick Desjardins 141k89 gold badges295 silver badges346 bronze badges 5
  • 4 This isn't JSON, it is just a JavaScript object. – Doug Neiner Commented Feb 8, 2010 at 20:15
  • 2 JSON = JavaScript Object Notation ... I guess we are talking the same language :P – Patrick Desjardins Commented Feb 8, 2010 at 20:17
  • 2 JSON is a serialized form of JavaScript objects and as such has slightly different syntax requirements. JSON is represented always as a string, not a real object. It is parsed into real objects, or serialized in a string. – Doug Neiner Commented Feb 8, 2010 at 20:18
  • I think the confusion comes in because array and object literal notation is (practically) equivalent to JSON. – Justin Johnson Commented Feb 8, 2010 at 20:20
  • 2 One thing to note is that a function would have no meaning in a JSON object since it's meant to be an interchange format. You can't serialize a JS function and have it make any sense to say, PHP or Java. – JAL Commented Feb 9, 2010 at 22:43
Add a comment  | 

2 Answers 2

Reset to default 10
var tempElement = {};
tempElement[ LOC.MENU_TITLE ] = function(menuItem, menu) {};

myMenu = [ tempElement ];

Addressing your edit. Try the following

var myMenu = [ {}, $.contextMenu.separator, {} ];

myMenu[0][ LOC.MENU_TITLE ]  = function(menuItem, menu) {};
myMenu[2][ LOC.MENU_TITLE2 ] = function(menuItem, menu) {};

JavaScript object literals expect the keys to be either an Identifier, a String literal or a Number literal, a workaround can be to build your object step by step, using the bracket notation:

var myMenu= [{}];
myMenu[0][LOC.MENU_TITLE] = function(menuItem,menu){ /*...*/ };

Note that the above isn't JSON.

JSON is a language-agnostic data interchange format, its grammar differs from the JavaScript Object literals, basically by allowing only string keys and the values must be an object, array, number, string, or the literal names: false, null true.

发布评论

评论列表(0)

  1. 暂无评论