javascript - Get X numbers from arrayA[5000] into arrayB[] in groups -
i have array this:
arraya = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2];
i need separated numbers in groups this:
arrayb = [1234,5678,9123,4567,8912];
as can see same arraya
in groups of 4 values new numbers.
i able make work bug this: arrayb=[undefined1234,undefined5678];
code:
for (var = 0; < 20; i++) { if (i/4== n+1){ arrayb[n] = temp; n++; } temp += arraya[i]; }
and thats it. understand bug, because of +=
not sure how other way.
this code trick
var arraya = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2,3,4]; var arrayb = []; (var x = 0; x < arraya.length; x += 4) { arrayb.push(arraya.slice(x, x + 4).join('')); } console.log(arrayb);
update
millie has raised fair point. if need numbers in result array, use following statement in for
loop
arrayb.push(parseint(arraya.slice(x, x + 4).join('')));
Comments
Post a Comment