In Javascript, using spread operator we can iteration an Array.
[...Array(5).keys()]
this gives a result ->
[0,1,2,3,4]
when I try to convert these number to a string
[...Array(5).keys().toString()]
this gives a result ->
["[", "o", "b", "j", "e", "c", "t", " ", "A", "r", "r", "a", "y", " ", "I", "t", "e", "r", "a", "t", "o", "r", "]"]
[...Array(10).keys().toString()]
this also gives a same result as above.
Why is this happening?
Array(5).keys()
is an Array Iterator. When you do
[...Array(5).keys().toString()]
you are not converting the keys to string, but you are trying to convert the iterator to String which gives you "[object Array Iterator]" as a character array.
You need to do:
[...Array(5).keys()].map(k => k.toString())
to convert all keys to String