我正在尝试从一个由许多其他数组组成的数组中提取信息。
我试图解决的问题是我试图从位于各种数组中的范围中提取数字,但在这样做时遇到了问题。
这是我的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/