0

Is there a way to convert "1,2" into 1,2 using native JS without some sort of wrapper?

"1,2".someMagic() \\ 1,2

Ultimately, I want to pass this as arguments to a function.

4
  • 1
    can you provide more example ? Commented Nov 8, 2015 at 3:57
  • Ideally, I want to call some function(1,2) by converting "1,2" from a string into args. Commented Nov 8, 2015 at 3:59
  • The exact thing you are trying to achieve is impossible. Think about it: func(1, 2) is parsed as a function call with two arguments. I.e. the engine already knows, before executing the code what, this is, just by parsing it. OTOH "1,2" is a string literal. You want to transform it to something else at runtime, i.e. after all the code has been parsed. You want to convert a runtime value to a syntax construct. Syntax cannot be changed or created at runtime. The only way to do that is to use eval, I guess that's not what you want to use. Commented Nov 8, 2015 at 4:06
  • @FelixKling I actually tried and it returns 2. eval would work for this but it's not working as expected. Commented Nov 8, 2015 at 4:15

6 Answers 6

1

Firstly, no there is no way to convert "1,2" to literally 1,2. Because it is invalid type. 1,2 is better represented as an array

You can use .apply like below to send 1,2 as parameters (array format) to the function someMagic

someMagic.apply(context, [1,2]);

Apply would call someMagic and send 1,2 as parameters

Sign up to request clarification or add additional context in comments.

Comments

1
function doSomething(param1, param2) {
    return parseInt(param1)+parseInt(param2);
};

doSomething.apply(this, "1,2".split(","));
// returns 3

Perhaps this thread Converting an array to a function arguments list may be of interest to you.

Comments

0

Using split is the answer.

var string = "1,2";
var splitString = string.split(","); //use , as an parameter to split

http://www.w3schools.com/jsref/jsref_split.asp

2 Comments

That converts to an array
And that array can then be passed to apply. someMagic.apply(null, string.split(","))
0

Similar to user3146092's answer, this one will not rely on your function having to parseInt.

someMagic.apply(this, '1,2'.split(',').map(function(n) { return parseInt(n, 10); }));

Comments

0

You can create an array of numbers and pass them as your arguments, that in fact, is the best way to do it in JavaScript.

var nums = "1,2,3"
  .split(",")
  .map(function (num) { return parseInt(num, 10) });

Now you can pass nums as your arguments.

Comments

0
var str = "1,2,3,4,5,6";

var arr=[];

function process(str){
  // split the string into tokens
     arr = str.split(",");

     // go through each array element
  arr.forEach(function(val,index,ar){
    // convert each element into integer
    var temp = parseInt(val);
    // repopulate array
    arr[index] = temp;
  });

}

process(str);

console.log(arr);

2 Comments

please add an explanation to your answer.
It's best if you add some commentary and not just toss code out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.