簡體   English   中英

根據用戶輸入索引數組中的字符

[英]Index characters from an array based on user input

我有一個從 a 到 z 的整個字母表的數組。 我也有一個輸入字段。 我希望能夠從字母數組的輸入字段中找到每個字符的索引,但我的函數不起作用。 我試過將輸入字段中的文本存儲到一個數組中,我也試過為它使用一個命名函數,但都沒有奏效。

<input type="text" id="plaintext" placeholder="Plaintext">
<div id="start"><div id="start_text">Start</div></div>
let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");
let plainParser = [];

startB.addEventListener('click', () => {
    for(let i=0; i < alph.length; i++){
        console.log(alph.findIndex( () => plainParser.push(plaintext.value.split(''))));
    };
});

不需要數組的快捷方式是使用每個字符的 charCode。

a從 97 開始

 const str = 'abc'; for(let s of str){ console.log(s.charCodeAt(0) - 97); }

我想……字母數組中的輸入字段中找到每個字符的索引

然后不是從 0 循環到 25:

for(let i=0; i < alph.length; i++){

您應該遍歷輸入中的每個字符:

for (let c of plaintext.value) {

我想……從字母數組中的輸入字段找到每個字符的索引

你有這個角色,所以找到索引:

alph.indexOf(c)

v'là.

let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");

startB.addEventListener('click', () => {
    for (let c of plaintext.value) {
        console.log(alph.indexOf(c));
    }
});

這是我認為您正在尋找的稍微重構的版本:

 const alphabet = 'abcdefghijklmnopqrstuvwxyz'; const result = document.querySelector(".result"); const plaintext = document.getElementById("plaintext"); const startB = document.querySelector(".start"); startB.addEventListener('click', () => { const source = plaintext.value; result.innerText = ''; if (!source) return; const indices = []; for (let char of source) { indices.push(alphabet.indexOf(char)); } result.innerText = indices.join(', '); });
 <input type="text" id="plaintext" placeholder="Plaintext"> <button class="start">Start</button> <div class="result" style="font-family: monospace;"></div>

這是一個在 keyup 事件上觸發並在每次擊鍵時將用戶輸入轉換為 Unicode 的演示。 如果您希望將用戶輸入轉換為 0 到 25 索引,則有一個帶有備用代碼的注釋。

 const az = document.forms[0]; const intArray = (node) => { return JSON.parse(JSON.stringify(`[${node.textContent}]`)); }; const dataKey = event => { const txt = event.target.value; const view = event.currentTarget.elements.view; /* swap lines if you want indexes 0 to 25 view.textContent += (event.which - 65) + ', '; */ view.textContent += event.which + ', '; }; az.onkeyup = event => { dataKey(event); let result = intArray(az.elements.view); console.log(result); };
 :root { font: 400 16px/1.3 Consolas } textarea, output { display: block; font: inherit } fieldset { width: fit-content; } #text { display: block; width: 98%; }
 <form id='az'> <fieldset> <legend>Unicode UTF-16</legend> <textarea id="text"></textarea> <output id='view'></output> </fieldset> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM