javascript 如何将整数转换为数字数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19182266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:41:22  来源:igfitidea点击:

How to convert an integer into an array of digits

javascript

提问by Psych Half

I want to convert an integer, say 12345, to an array like [1,2,3,4,5]. I have tried the below code, but is there a better way to do this?

我想将一个整数,比如说12345,转换成一个像[1,2,3,4,5]. 我已经尝试了下面的代码,但有没有更好的方法来做到这一点?

var n = 12345;
var arr = n.toString().split('') ;
for(i=0;i<arr.length;i++) arr[i] = +arr[i]|0 ;

采纳答案by Bergi

I'd go with

我愿意

var arr = n.toString(10).replace(/\D/g, '0').split('').map(Number);

You can omit the replaceif you are sure that nhas no decimals.

replace如果您确定n没有小数,则可以省略。

回答by Juanma Menendez

**** 2019 Answer****

**** 2019 答案****

This single line will do the trick:

这一行就可以解决问题:

Array.from(String(12345), Number);

Example

例子

const numToSeparate = 12345;

const arrayOfDigits = Array.from(String(numToSeparate), Number);

console.log(arrayOfDigits);   //[1,2,3,4,5]

Explanation

解释

1- String(numToSeparate)will convert the number 12345 into a string, returning '12345'

1-String(numToSeparate)将数字 12345 转换为字符串,返回 '12345'

2- The Array.from()method creates a new Array instance from an array-like or iterable object, the string '12345' is an iterable object, so it will create an Array from it.

2- 该Array.from()方法从类数组或可迭代对象创建一个新的 Array 实例,字符串 '12345' 是一个可迭代对象,因此它将从中创建一个 Array。

3- But, in the process of automatically creating this new array, the Array.from()method will first pass any iterable element (every character in this case eg: '1', '2') to the function we set to him as a second parameter, which is the Numberfunction in this case

3- 但是,在自动创建这个新数组的过程中,该Array.from()方法将首先将任何可迭代元素(本例中的每个字符,例如:'1'、'2')传递给我们设置给他的函数作为第二个参数,这是Number在这种情况下的功能

4- The Numberfunction will take any string character and will convert it into a number eg: Number('1'); will return 1.

4- 该Number函数将接受任何字符串字符并将其转换为数字,例如:Number('1'); 会回来1

5- These numbers will be added one by one to a new array and finally this array of numbers will be returned.

5- 这些数字将一个一个地添加到一个新数组中,最后返回这个数字数组。

Summary

概括

The code line Array.from(String(numToSeparate), Number);will convert the number into a string, take each character of that string, convert it into a number and put in a new array. Finally, this new array of numbers will be returned.

代码行Array.from(String(numToSeparate), Number);会将数字转换为字符串,获取该字符串的每个字符,将其转换为数字并放入一个新数组中。最后,将返回这个新的数字数组。

回答by LoremIpsum

var n = 12345;
var arr = ('' + n).split('').map(function(digit)  {return +digit;});

The map function, though, is only supported by recent browsers.

但是,地图功能仅受最近的浏览器支持。

回答by michel404

I'd do this, to avoid using strings when you don't need them:

我会这样做,以避免在不需要字符串时使用字符串:

var n = 12345;
var arr = [];
while(n>0){arr.unshift(n%10);n=n/10|0;}

回答by Jayampathy Wijesena

I have fist convert the number to a string and then used Array.from() to convert the string to an array.

我首先将数字转换为字符串,然后使用 Array.from() 将字符串转换为数组。

let dataArray = Array.from(value.toString());

回答by Penny Liu

Runnable snippet:

可运行片段:

const n = 12345;
let toIntArray = (n) => ([...n + ""].map(Number));
console.log(toIntArray(n));

回答by Kriti Aggarwal

First, the integer is converted string & then to array. Using map function, individual strings are converted to numbers with the help of parseInt function. Finally, that array is returned as the result.

首先,整数被转换为字符串,然后转换为数组。使用 map 函数,在 parseInt 函数的帮助下,单个字符串被转换为数字。最后,该数组作为结果返回。

const digitize = n => [...`${n}`].map(i => parseInt(i));