简体   繁体   English

根据用户输入索引数组中的字符

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

I have an array of the entire alphabet from a to z.我有一个从 a 到 z 的整个字母表的数组。 I also have an input field.我也有一个输入字段。 I want to be able to find the index of each character from the input field in the alphabet array but my function doesn't work.我希望能够从字母数组的输入字段中找到每个字符的索引,但我的函数不起作用。 I've tried storing the text from the input field into an array, and I've tried using a named function for it as well but neither worked.我试过将输入字段中的文本存储到一个数组中,我也试过为它使用一个命名函数,但都没有奏效。

<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(''))));
    };
});

A shortcut without needing the array is use the charCode of each character.不需要数组的快捷方式是使用每个字符的 charCode。

a starts at 97 a从 97 开始

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

I want to … find the index of each character from the input field in the alphabet array我想……字母数组中的输入字段中找到每个字符的索引

Then instead of looping from 0 to 25:然后不是从 0 循环到 25:

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

you should loop over every character from the input:您应该遍历输入中的每个字符:

for (let c of plaintext.value) {

I want to … find the index of each character from the input field in the alphabet array我想……从字母数组中的输入字段找到每个字符的索引

You have the character, so find the index:你有这个角色,所以找到索引:

alph.indexOf(c)

v'là. 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));
    }
});

Here's a slightly refactored version of what I think you are looking for:这是我认为您正在寻找的稍微重构的版本:

 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>

Here's a demo that fires on keyup event and converts user input to Unicode on every keystroke.这是一个在 keyup 事件上触发并在每次击键时将用户输入转换为 Unicode 的演示。 There is a comment with alternate code if you want to have user input converted to 0 to 25 index instead.如果您希望将用户输入转换为 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