将字符串转成数组的方法

// 遍历字符串
let str = 'hello'
for (let i of str) {
  console.log(i);
}
// 将字符串转成数组
// split() 方法用于把一个字符串分割成字符串数组。
let arr1 = str.split('');
// Array.from() 方法从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。
let arr2 = Array.from(str);
// 拓展运算符使用三个点'...'表示。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列
let [...arr3] = str;
console.log('------', arr1, arr2, arr3);

将字符串转成数组的方法_第1张图片

你可能感兴趣的:(H5,javascript)