I want to create an integer or a number that contains all the digits from an array of numbers or strings. How can i achieve this?
eg:
digitArry = [9', '8', '7', '4', '5', '6'];
should become
integer = 987456;
You can use join and parseInt :
join
parseInt
var digitArry = ['9', '8', '7', '4', '5', '6']; var integer = parseInt(digitArry.join(''), 10); console.log(integer);
EDIT: As @kay suggested, another alternative uses + to convert a string to a number:
+
var digitArry = ['9', '8', '7', '4', '5', '6']; var integer = +digitArry.join(''); console.log(integer);
Source: https://habr.com/ru/post/1269533/More articles:Android Client Certificate Https - androidfirebase cloud function will not store cookie other than "__session" - javascriptSparklyr - Decimal precision 8 exceeds maximum precision 7 - rIs a for loop always executed at least once? - c ++Is there a reason why C ++ 11+ std :: mutex should be declared as a global variable instead of passed to std :: thread as a function parameter? - c ++Partially fill an object when it hangs - javascriptCode issue: finding a dividend in a list - pythonStacked LTS Versions - haskellClass not found in module - kotlinGetting random number from assembler 6502 - assemblyAll Articles