I can't see why this code doesn't produce numbers. Can anyone explain please?
a = '1 3 2 6 1 2'.split(' ');
a = a.map(Number);
for (item in a){
console.log(typeof(item));
}
The output for me in Chrome is 6 strings.
You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.
map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array.
for..of
, not for..in
to iterate Arrays:arr = '1 2 3 4'.split(' ').map(Number) // ["1","2","3","4"] => [1,2,3,4]
for( item of arr ) console.log( typeof(item), item )
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