Using eval() in Javascript to unpack the array - Stack Overflow

I have an array that I need to unpack. So, from something like var params = new Array();params.push(&q

I have an array that I need to unpack.

So, from something like

var params = new Array();
params.push("var1");
params.push("var2");

I need to have something like

"var1", "var2".

I tried using eval, but eval() gives me something like var1, var2...i don't want to insert quotes myself as the vars passed can be integers, or other types. I need to pass this to a function, so that's why i can't just traverse the array and shove it into a string.

What is the preferred solution here?

I have an array that I need to unpack.

So, from something like

var params = new Array();
params.push("var1");
params.push("var2");

I need to have something like

"var1", "var2".

I tried using eval, but eval() gives me something like var1, var2...i don't want to insert quotes myself as the vars passed can be integers, or other types. I need to pass this to a function, so that's why i can't just traverse the array and shove it into a string.

What is the preferred solution here?

Share Improve this question asked May 25, 2010 at 22:00 sarsnakesarsnake 27.8k62 gold badges185 silver badges295 bronze badges 5
  • I doubt you can get JavaScript to add the " itself, unless maybe you use JSON.stringify(). " are just the string delimiters and do not belong to the string value. – Dormilich Commented May 25, 2010 at 22:06
  • that's the thing, is that i would like to pass the VALUES of the vars...they may not always be strings either. I am wondering if it's possible at all. – sarsnake Commented May 25, 2010 at 22:16
  • How do you want "other types" to be serialised? – James Commented May 25, 2010 at 22:17
  • 1 When you say "pass the values"... where are you passing to? – James Commented May 25, 2010 at 22:18
  • I'm trying to e up with a general solution, so at the point of passing I won't know how many parameters the function will accept. Hence, I shove them into the array. Then, I need to unpack and pass them. So the variables shoved into the array may all be different types:strings, ints, other arrays. When unpacking, I want to be able to do something like: DoSomething(param1, param2, param3) where param1 .. param3 e from the array. I posted here: stackoverflow./questions/2836037/… got no real answers – sarsnake Commented May 25, 2010 at 22:27
Add a ment  | 

2 Answers 2

Reset to default 7

If you have an array of values that you want to pass to a function filling the formal parameters then you can use the apply method of the Function prototype.

var arr = [1, 2, "three"];

function myFunc(a, b, c) {
   // a = 1, b = 2, c = "three"
   ...
}

myFunc.apply(this, arr);

By the way, the this parameter in the last statement can be set to any object to set the value of this inside myFunc

This generates the output you want

var params = new Array();
params.push("var1");
params.push("var2");

var s = "\"" + params.join("\",\"") + "\"";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745038512a4607668.html

相关推荐

  • Using eval() in Javascript to unpack the array - Stack Overflow

    I have an array that I need to unpack. So, from something like var params = new Array();params.push(&q

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信