将逗号分隔的整数(NOT字符串)转换为数组JS

时间:2018-05-30 20:43:34

标签: arrays numbers integer

这似乎是一个非常简单的问题,但我无法找到解决方案。 我需要将这个函数的参数(只是数字后面跟一个逗号)转换为一个充满数字的数组。问题是,参数不是字符串,我无法添加""。如果我console.log([card])只有第一个数字出现在数组中(2)...我试着做一个循环,但它没有成功。 谢谢你的帮助!

function cc(card) {

}
cc(2,3,4,5,6);

1 个答案:

答案 0 :(得分:1)

无论如何,为了以防万一,您可以使用arguments变量:

var cc = function () { console.log(arguments) }

cc(1,2,3,4,5)打印Arguments { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, … }cc(2,3,4,5,6,7,8)打印Arguments { 0: 1, 1: 2, 2: 3, … }

来源:https://gomakethings.com/getting-an-array-of-all-arguments-passed-into-a-function-with-vanilla-javascript/