jQuery使数组键可变 [英] Jquery Make Array Key To Be Var

查看:240
本文介绍了jQuery使数组键可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在其函数上执行其add数组,并将键设为var键的示例,例如,我将其发送至函数:

What I'm trying to do its add array on my function and the key to be var key, example I send this to my function:

 var obj = {
  "div_id":"#Menu_Videos",
  "div_class":".wideDropdown_Menu_Videos",
 };
 wideDropdown(obj);

我的功能是

 function wideDropdown(obj) {
  $.each(obj, function(key, value) {
   var key = value;
  });
 }

但无法正常工作,如何使函数成为示例

but its not working how I can make the key on my function to be example

 var div_id = "#Menu_Videos",

更新

我需要类似此命令的内容才能包含在我的函数中

I need something like this command to i can include on my function

var obj[key] = obj[value];

推荐答案

您可以使用 eval ,尽管通常使用eval是一个坏主意.

You can do this with eval, although in general using eval is a bad idea.

$.each(obj, function (key, value) {
    eval(key + '=' + '"' + value + '"');
});

根据MDN:

eval()函数的参数是一个字符串.如果该字符串表示一个表达式,则eval()对该表达式求值.如果参数代表一个或多个JavaScript语句,则eval()执行这些语句.不要调用eval()来计算算术表达式; JavaScript自动计算算术表达式.

The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() performs the statements. Do not call eval() to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

这篇关于jQuery使数组键可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆