2017-01-17 39 views
1

(必须阅读才能确切理解我需要的) 我想创建一个需要输入数字的程序,例如:12345,然后将此数字拆分为2位数数字并将其存储在一个数组中。数组必须如下所示:[0] = 45 [1] = 23 [2] = 1。这意味着数字的分割必须从数字的最后一位开始,而不是第一位。将整数分成2位数并放入数组中Javascript

这是我到现在为止:

var splitCount = []; // This is the array in which we store our split numbers 
 
//Getting api results via jQuery's GET request 
 
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) { 
 
    //result is our api answer and contains the recieved data 
 
    //now we put the subscriber count into another variable (count); this is just for clarity 
 
    count = result.items[0].statistics.subscriberCount; 
 
    //While the subscriber count still has characters 
 
    while (count.length) { 
 
     splitCount.push(count.substr(0, 2)); //Push first two characters into the splitCount array from line 1 
 
     count = count.substr(2); //Remove first two characters from the count string 
 
    }  
 
    console.log(splitCount) //Output our splitCount array 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

,但这样做的问题是,如果有,例如5个位数:12345的最后一位数字会是一个数组由本身是这样的:[0] = 12 [1] = 34 [2] = 5,但我需要的最后阵列具有2位和第一应该是一个与一个数字代替这样的:[0] = 1 [ 1] = 23 [2] = 45

+0

尝试从字符串 – dex412

+0

但其int的端部开始?你可以帮我吗? – Kenneth

+0

将它连接到一个“”,然后通过paresInt()方法返回到一个int – Seraf

回答

3

分割字符串,通过使用正则表达式不同奇数和偶数字符串长度,Array#map它使用Number,并Array#reverse数组:

function splitToNumbers(str) { 
 
    return str.match(str.length % 2 ? /^\d|\d{2}/g : /\d{2}/g).map(Number).reverse() 
 
} 
 
    
 
console.log(splitToNumbers('1234567')); 
 

 
console.log(splitToNumbers('123456'));

0

修改您的代码有点

var splitCount = []; // This is the array in which we store our split numbers 
 
//Getting api results via jQuery's GET request 
 
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) { 
 
    //result is our api answer and contains the recieved data 
 
    //now we put the subscriber count into another variable (count); this is just for clarity 
 
    count = result.items[0].statistics.subscriberCount; 
 
    //While the subscriber count still has characters 
 
    while (count.length) { 
 
     splitCount.push(count.substr(-2)); //Push last two characters into the splitCount array from line 1 
 
     if(count.length > 1) { 
 
      count = count.substr(0, count.length - 2); //Remove first last two characters from the count string 
 
     } else { 
 
      break; 
 
     } 
 
    }  
 
    console.log(splitCount) //Output our splitCount array 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

该代码似乎不适合我? – Kenneth

+0

sry,错误的变量名称 – Ayman

1

这里是一个片段,履行是任务。该函数的值存储在阵列中myArray,然后在阵列的<p>元件输出。该号码输入到输入框中。随后可以随时更改。

function myFunction() { 
 
    // Input field 
 
    var input = document.getElementById('num');1 
 
    // Length of the array storing the numbers 
 
    var myArraySize = Math.floor(input.value.length/2) + (input.value.length % 2); 
 
    // The array storing the numbers 
 
    var myArray = []; 
 
    
 
    for (var i = 0; i < myArraySize; i++) { 
 
    myArray[i] = input.value.slice(2*i,2*i+2); 
 
    } 
 
    // Output the array 
 
    document.getElementById('demo').innerHTML = myArray; 
 
}
<input id="num" type="text" onkeyup="myFunction()" /> 
 
<p id="demo">Result</p>

1

您可能分裂与正则表达式的字符串和反向阵列。

这个答案在很大程度上受到这个启发answer

var regex = /(?=(?:..)*$)/; 
 

 
console.log('12345'.split(regex).reverse()); 
 
console.log(''.split(regex).reverse());
.as-console-wrapper { max-height: 100% !important; top: 0; }

1

无需正则表达式或昂贵的计算。你可以简单地做如下:

var n = 250847534, 
 
steps = ~~Math.log10(n)/2, 
 
    res = []; 
 
for(var i = 0; i <= steps; i++) { 
 
    res.push(Math.round(((n /= 100)%1)*100)); 
 
    n = Math.trunc(n); 
 
} 
 
console.log(res);

0

有几种方式来处理你的问题,从漂亮的单行使用正则表达式(如Ori DoriNina Scholz),以Redu's answer直接作用于数字,避免串。

按照您的代码示例和注释的逻辑,这里是一个替代方案,将数字转换为字符串,向后循环以一次提取两位数字,将这些数字转换为数字(使用一元加运算符),并输出该号码结果数组:

function extract(num) { 
 
    var s = '0' + num, //convert num to string 
 
    res = [], 
 
     i; 
 
    for(i = s.length; i > 1; i -= 2) { 
 
    //loop from back, extract 2 digits at a time, 
 
    //output as number, 
 
    res.push(+s.slice(i - 2, i)); 
 
    } 
 
    return res; 
 
} 
 

 
//check that 12345 outputs [45, 23, 1] 
 
console.log(extract(12345)); 
 

 
//check that 123456 outputs [56, 34, 12] 
 
console.log(extract(123456));

相关问题