javascript - split method of array
学习ing
学习ing 2017-06-30 09:57:12
0
4
993

'1,2,3,4,5'.split(',');
The above code will output ['1','2','3','4','5'] ;
Each value in the array is of string type. Is there any good way to make the values ​​in the split array become number type?

学习ing
学习ing

reply all(4)
ringa_lee

You can only traverse it again after splitting

代言
var number = '1,2,3,5,4,9,8,7'.split(','),
    out = [];
for(var i=0;i<number.length;i++){
    out.push(+number[i])
}
console.log(out)
[1, 2, 3, 5, 4, 9, 8, 7]
给我你的怀抱

const a ='123456';
a.split(' ').map (i => Number(i))

刘奇

The type of your split ('1,2,3,4,5') is originally a string, just a string composed of numbers. Therefore, it is right for the array formed after split to maintain its original type. Because it is not a numeric type. To change it to number type, just iterate through the array and convert each element.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!