2017-01-17 48 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));

相關問題