javascript 如何在Javascript中将数字拆分为其数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30025888/
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
How to split a number into its digits in Javascript?
提问by Roberto
I want to Split a number into its digit (for example 4563 to 4 , 5 , 6 , 3 ) then addiction this digits. (for example: 4+5+6+3=18)
我想将一个数字拆分成它的数字(例如 4563 到 4 , 5 , 6 , 3 )然后沉迷于这些数字。(例如:4+5+6+3=18)
I can write code for 3 digit or 2 digit and ... numbers seperately but I cant write a global code for each number.
我可以分别为 3 位或 2 位和 ... 数字编写代码,但我无法为每个数字编写全局代码。
so this is my code for 2 digit numbers:
所以这是我的 2 位数代码:
var a = 23
var b = Math.floor(a/10); // 2
var c = a-b*10; // 3
var total = b+c; // 2+3
console.log(total); // 5
and this is my code for 3 digit numbers:
这是我的 3 位数代码:
var a = 456
var b = Math.floor(a/100); // 4
var c = a-b*100; // 56
var d = Math.floor(c/10); // 5
var e = c-d*10; // 6
var total = b+d+e; // 4+5+6
console.log(total); // 15
but I cant write a code to work with each number.How can I write a global code for each number?
但我无法编写代码来处理每个数字。如何为每个数字编写全局代码?
回答by Arun P Johny
In modern browsers you can do an array operation like
在现代浏览器中,您可以执行数组操作,例如
var num = 4563;
var sum = ('' + num).split('').reduce(function (sum, val) {
return sum + +val
}, 0)
Demo: Fiddle
演示:小提琴
where you first create an array digits then use reduce to sum up the values in the array
首先创建一个数组digits然后使用reduce来总结数组中的值
回答by Feyyaz
var num = 4563;
var sum = 0;
while(num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
console.log(sum);
回答by steo
This solution converts the number to string, splits it into characters and process them in the callback function (prev
is the result from the previous call, current
is the current element):
本方案将数字转为字符串,拆分为字符,在回调函数中处理(prev
是上次调用的结果,current
是当前元素):
var a = 456;
var sum = a.toString().split("").reduce(function(prev, current){
return parseInt(prev) + parseInt(current)
})
回答by Bhavik Shah
Do number%10(modulus) and then number/10(divide) till the number is not 0
做 number%10(modulus) 然后 number/10(divide) 直到数字不为 0
回答by The KNVB
I hope the following example is useful to you:
我希望下面的例子对你有用:
var text="12345";
var total=0;
for (i=0;i<text.length;i++)
{
total+= parseInt(text[i]);
}
alert(total);
回答by Ago Chinedum
Here is how I would approach the problem. The trick I used was to split on the empty string to convert the string to an array and then use reduce
on the array.
这是我将如何解决这个问题。我使用的技巧是拆分空字符串以将字符串转换为数组,然后reduce
在数组上使用。
function digitSum(n) {
// changes the number to a string and splits it into an array
return n.toString().split('').reduce(function(result, b){
return result + parseInt(b);
}, 0);
}
As mentioned by several other posters (hat tip to my commenter), there are several other good answers to this question as well.
正如其他几张海报(给我的评论者的帽子提示)所提到的,这个问题还有其他几个很好的答案。
回答by Numinous Cranium
Here is my solution using ES6 arrow functions as call back. - Convert the number into a string. - Split the string into an array. - Call the map method on that array. - Callback function parse each digit to an array.
这是我使用 ES6 箭头函数作为回调的解决方案。- 将数字转换为字符串。- 将字符串拆分为数组。- 在该数组上调用 map 方法。- 回调函数将每个数字解析为一个数组。
let number = 65535;
//USING MAP TO RETURN AN ARRAY TO DIGITS
let digits = number.toString()
.split("")
.map(num => parseInt(num));
//OUTPUT TO DOM
digits.forEach(
digit =>
document.querySelector("#out").innerHTML += digit + "<br>"
);
<p id="out"></p>
回答by Mixalloff
1) You can cast input number to string, using .toString() method and expand it into array with spread (...) operator
1) 您可以使用 .toString() 方法将输入数字转换为字符串,并使用扩展 (...) 运算符将其扩展为数组
const splitNumber = n => [ ...n.toString() ]
2) Another short way - using recursion-based solution like:
2)另一种简短的方法 - 使用基于递归的解决方案,例如:
const splitNumber = n => n ? [ ...splitNumber(n/10|0), n%10 ] : []