JS 字符串转数组
方法一:
1 var s = '1231231'; 2 var arr = []; 3 for (a = 0; a < s.length; a++) { 4 arr.push(parseInt(s[a])) 5 } 6 console.log(arr)
方法二:
方法三(最简化):
1 var s = '1231231'; 2 var b = []; 3 for (a = 0; a < s.length; a++) { 4 b.push(s[a]) 5 } 6 var arr = b.map(Number) 7 console.log(arr) //[1, 2, 3, 1, 2, 3, 1]
var s = '1231231'; var arr = s.split('').map(Number); console.log(arr) //[1, 2, 3, 1, 2, 3, 1]
方法四(Vue):
let str = '132131'; let str1 = Array.from(str); let arr = []; str1.forEach(function(item, index, str1) { arr.push(item * 1) //隐式转换 return arr; }) console.log(arr);
本文来自博客园,作者:火知火味,转载请注明原文链接:https://www.cnblogs.com/whwa/p/13684152.html