将数组中的字符串拆分为子字符串

时间:2019-04-09 00:20:39

标签: javascript

我有一个非常简单的问题。我有一个用户输入,并将来自用户输入的文本推入数组,然后(理论上)将其转换为字符串,然后将其拆分为每个字符串中每个字符的数组。我的问题是如何将数组中的字符串拆分为1个字符长的数组。

let plaintext = document.getElementById("plaintext");
let startB = document.getElementById("start");

let plain = [];
let encryptStorage = [];

startB.addEventListener('click', () => {
  plain.push(plaintext.value);
  plain.toString();
  encryptStorage.push(plain.split(''));
  console.log(encryptStorage);
});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>CryptoMatic</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <input type="text" id="plaintext" placeholder="Plaintext">
  <div id="start">
    <div id="startT">Start</div>
  </div>
  <script src="app.js"></script>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

您不需要调用.toString()或数组。只需直接使用该值即可:

const startB = document.querySelector("#start");

const encryptStorage = [];

startB.addEventListener('click', () => {
    const plaintext = document.querySelector('#plaintext');

    encryptStorage.push(plaintext.value.split(''));
    console.log(encryptStorage);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CryptoMatic</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <input type="text" id="plaintext" placeholder="Plaintext">
                <div id="start">
                <div id="startT">Start</div></div>
       <script src="app.js"></script>
    </body>
</html>