最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Splitting a number into an array - Stack Overflow

programmeradmin5浏览0评论

Alright so I'm trying to pletely split a number into an Array. So for example:

var num = 55534;
var arr = []; <- I would want the Array to look like this [5,5,5,3,4] 

Basically i want to to pletely split the number apart and place each Number into its own element of the array. Usually in the past i would just convert the number into a string then use the .split() function. This is how i use to do it:

num += "";
var arr = num.split("");

But this time i actually need to use these numbers, so they can not be strings. What would you guys say be the way of doing this?

Update, after the edit for some reason my code is crashing every run:

function DashInsert(num) { 

  num += "";
  var arr = num.split("").map(Number); // [9,9,9,4,6]
  for(var i = 0; i < arr.length; i++){
    if(arr[i] % 2 === 1){
    arr.splice(i,0,"-");
    }// odd
  }
  return arr.join(""); 

}

Alright so I'm trying to pletely split a number into an Array. So for example:

var num = 55534;
var arr = []; <- I would want the Array to look like this [5,5,5,3,4] 

Basically i want to to pletely split the number apart and place each Number into its own element of the array. Usually in the past i would just convert the number into a string then use the .split() function. This is how i use to do it:

num += "";
var arr = num.split("");

But this time i actually need to use these numbers, so they can not be strings. What would you guys say be the way of doing this?

Update, after the edit for some reason my code is crashing every run:

function DashInsert(num) { 

  num += "";
  var arr = num.split("").map(Number); // [9,9,9,4,6]
  for(var i = 0; i < arr.length; i++){
    if(arr[i] % 2 === 1){
    arr.splice(i,0,"-");
    }// odd
  }
  return arr.join(""); 

}
Share Improve this question edited Aug 14, 2014 at 3:20 SuperCoolDude1337 asked Aug 14, 2014 at 3:05 SuperCoolDude1337SuperCoolDude1337 952 silver badges9 bronze badges 1
  • Your code is adding an extra element, "-", to the array every iteration (arr.splice(i, 0, "-") deletes zero elements and then adds "-" at position i). Hence it will never finish the iteration—your for loop is infinite. Note that it is reading the same element over and over, because the added "-" means that the element it was reading is shifted over one index, and will be read again next iteration. – Fengyang Wang Commented Aug 14, 2014 at 3:23
Add a ment  | 

3 Answers 3

Reset to default 5
String(55534).split("").map(Number)

...will handily do your trick.

You can do what you already did, and map a number back:

55534..toString().split('').map(Number)
//^ [5,5,5,3,4]

I'll do it like bellow

var num = 55534;
var arr = num.toString().split(''); //convert string to number & split by ''
var digits = arr.map(function(el){return +el}) //convert each digit to numbers
console.log(digits); //prints [5, 5, 5, 3, 4]

Basically I'm converting each string into numbers, you can just pass Number function into map also.

发布评论

评论列表(0)

  1. 暂无评论