javascript - 如何从一系列数组中搜索输入标签值

标签 javascript css arrays html

我正在尝试从一个由许多其他数组组成的数组中提取信息。

我试图解决的问题是我试图从位于各种数组中的范围中提取数字,但在这样做时遇到了问题。

这是我的JS代码

我用 Javascript 做了一个 range() 函数,它列出了包含的参数的(开始,结束)

      function rangeSCORE(start, end) {
            return Array(end - start + 1).fill().map((_, idx) => start + idx)
        }


const score = document.getElementById('score').value;

const levelOneScore = rangeSCORE(486, 489);
const levelTwoScore = rangeSCORE(490, 493);
const levelThreeScore = rangeSCORE(494, 497);
const levelFourScore = rangeSCORE(498, 501);
const levelFiveScore = rangeSCORE(502, 505);
const levelSixthScore = rangeSCORE(506, 509);
const levelSeventhScore = rangeSCORE(510, 513);
const levelEightScore = rangeSCORE(514, 517);
const levelNinthScore = rangeSCORE(518, 528);

const ScoreValue = [levelOneScore, levelTwoScore, levelThreeScore, levelFourScore, levelFiveScore, levelSixthScore, levelSeventhScore, levelEightScore, levelNinthScore];

function showChance() {
    if(ScoreValue.includes(score)) {
        console.log(score)
    }
}

这是我的 HTML 代码

<input type="number" class="transparentBar" min="472" max="528" id="score" placeholder="SCORE">

例如,如果我输入值并单击一个按钮,我想知道如何搜索数组列表

最佳答案

您会受益于不同的数据结构。您想要将分数映射到级别,因此最简单的方法是创建一个数组,其中索引表示分数,相应的数组值表示级别。

你可以像这样创建这样一个数组:

const scoreToLevel = [].concat(...
    [485, 489, 493, 497, 501, 505, 509, 513, 517, 528].map((end, i, ends) =>
        Array(end - (ends[i-1] || -1)).fill(i)
    )
);


// I/O handling:
const scoreInput = document.getElementById('score');
const levelOutput = document.getElementById('level');
scoreInput.addEventListener("input", function showChance() {
    levelOutput.textContent = scoreToLevel[this.value];
});
Score: <input type="number" class="transparentBar" min="472" max="528" id="score" placeholder="SCORE"><br>
Level: <span id="level"></span>

请注意,此处未指定范围的起点,因为它似乎总是与前一个范围的终点相邻。

关于javascript - 如何从一系列数组中搜索输入标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53973737/

相关文章:

c++ - 末端指针会与其他对象重叠吗?

javascript - 向从 Mage::getBlockSingleton 返回的对象添加 html 属性

asp.net - 我需要什么 CSS 样式才能使只读文本框控件看起来已禁用

arrays - 从未知维度的多维数组中获取元素

javascript - 将对象拆分成 block

css - 内联无序列表

javascript - 如何更改请求的 header ?

javascript - 想要使用 Ajax 加载下拉列表中所选选项的图像和一些文本

javascript - XMLHTTP ReadyState 3 未在 Webkit 中更新

html - 我的标题图片在向下滚动之后