我正在尝试从由许多其他数组组成的数组中提取信息。
我要解决的问题是我试图从位于各种数组中的范围中提取一个数字,但这样做却遇到了麻烦。
这是我的JS代码
我用Java语言制作了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">
例如,如果我输入值并单击一个按钮,我想知道如何搜索数组列表
答案 0 :(得分:1)
您将受益于不同的数据结构。您想要将分数映射到一个级别,所以最简单的方法是创建一个数组,其索引代表分数,而相应的数组值代表级别。
您可以创建这样的数组:
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>
请注意,此处未指定范围的起点,因为它似乎总是与上一个范围的终点相邻。
答案 1 :(得分:0)
您将rangeSCORE定义为2D数组。因此,尝试此操作,它将搜索rangeSCORE内的任何得分。
function rangeSCORE(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
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() {
const score = document.getElementById('score').value;
for(var i=0;i<ScoreValue.length;i++)
{
for(var j=0;j<ScoreValue[i].length;j++){
if (ScoreValue[i][j]==score){
console.log(score);
console.log("level",i);
}
}
}
}
<input type="number" class="transparentBar" min="472" max="528" id="score" placeholder="SCORE" onchange=showChance()>