var a = 12345678; a= a.toString(); var arr=[]; for (var i =0; i<a.length-1; i++) { arr.push(Number(a[i]+''+a[i+1])); } console.log(arr);方法二:
var str = "12345678", length = str.length - 1, output = Array.from({ length }, (_,i) => +str.slice(i, i+2)) console.log(output)方法三:
function getChunks(number, size) { let str = number.toString(), length = str.length - size + 1; return Array.from({ length }, (_,i) => +str.slice(i, i + size)) } console.log(getChunks(12345, 3)) console.log(getChunks(12345678, 2))方法四:
var a = 12345678; const result = a.toString().split("").reduce((acc, ele, idx, arr) => { return arr[idx + 1] ? acc.concat(+(ele + arr[idx + 1])) : acc; }, []); console.log(result); console.assert(result, [12,23,34,45,56,67,78]);
var str ='12345678'; const eleven = string => { let result = []; let singles = string.split(''); let first = Number(singles.splice(0, 2).join('')); for (let i = 0; i < string.length-1; i++) { let next = 11 * i; result.push(first+next); } return result; } console.log(eleven(str));
输出结果: