I want to create an integer or number that contains all the digits from an array of digits or string. How can I achieve that ?
for example:
digitArry = [9', '8', '7', '4', '5', '6'];
should become
integer = 987456;
You can use join
and parseInt
:
var digitArry = ['9', '8', '7', '4', '5', '6'];
var integer = parseInt(digitArry.join(''), 10);
console.log(integer);
EDIT: As suggested by @kay, another alternative is using +
to convert string to number:
var digitArry = ['9', '8', '7', '4', '5', '6'];
var integer = +digitArry.join('');
console.log(integer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With