(必须阅读才能确切理解我需要的) 我想创建一个需要输入数字的程序,例如: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