字符串转换为数组

66 阅读1分钟

1、split() 方法

<script>
      const text = "abcd";
      const arr = text.split("");
      console.log(arr); //['a', 'b', 'c', 'd']
</script>

2、展开运算符

<script>
      const text = "abcd";
      const arr = [...text];
      console.log(arr); //['a', 'b', 'c', 'd']
</script>

3、解构赋值

<script>
      const text = "abcd";
      const [...arr] = text;
      console.log(arr); //['a', 'b', 'c', 'd']
</script>

4、Array.from()

    <script>
      const text = "abcd";
      const arr = Array.from(text);
      console.log(arr); //['a', 'b', 'c', 'd']
    </script>