How can I concatenate an array of numbers into 1 cascading number?
6 answers
I can't think of anything else but
+Function.call.apply(String.prototype.concat, x)
or if you insist
+''.concat.apply('', x)
In ES6:
+''.concat(...x)
Usage reduce
:
+x.reduce((a, b) => a + b, '');
Or if you prefer
x.reduce(Function.call.bind(String.prototype.concat), '')
Another idea is to manipulate the array like a string, always a good approach.
+String.prototype.replace.call(x, /,/g, '')
There may be other ways. Perhaps a Google search on "javascript array concatenation" will trigger some obscure function that concatenates the array elements.
+4
user663031
source
to share