在Javascript中将数字转换为数组 [英] Convert number into array in Javascript
问题描述
当我输入一个带有提示符 var myNumber = parseInt(提示符(...))
的数字时,我想将它转换为数字数组。当我尝试 myNumber = myNumber.split()
时,它返回:
When I type a number with prompt var myNumber = parseInt(prompt("..."))
I want it to be converted into an array of numbers. When I try with myNumber = myNumber.split("")
it returns:
对象1没有方法'拆分'
Object 1 has no method 'split'
请帮助我
推荐答案
你不能拆分int,你需要有一个字符串数据类型才能使这个代码有效!
You cannot split the int, you need to have a string data type for this code to work!
所以,我想要建议首先拆分它,然后将其转换为int
So, I would like to suggest to first split it and then convert it to int as
var numbers = "1, 2, 3";
var eachNumber = numbers.split(",");
/* now parse them or whatso ever */
这将有效,因为你'只是拆分字符串。然后你将按照第一种方法(你的方法)解析它。
This will work, as you're just splitting the string. And then you will parse it just the way you did it in the first method (of yours).
这篇关于在Javascript中将数字转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!