(必須閱讀才能確切理解我需要的) 我想創建一個需要輸入數字的程序,例如: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
嘗試從字符串 – dex412
但其int的端部開始?你可以幫我嗎? – Kenneth
將它連接到一個「」,然後通過paresInt()方法返回到一個int – Seraf