gpt4 book ai didi

javascript - 根据键码更改数组

转载 作者:行者123 更新时间:2023-12-03 12:13:20 27 4
gpt4 key购买 nike

我尝试使用下面的代码使 arrayGroup 包含与向上或向下击键相关的数据集。根据这一点,它将从名为 numberArray1 的变量中加或减 1,并允许根据 arrayGroup 设置为哪个数据集来调用不同的数组。目前,我可以将数组作为字符串调用,但在将它们作为数组调用并将它们传递到 arrayGroup 时遇到问题。

您的建议将不胜感激!

var arrayGroup = [];

var numberArray1 = [1,2,3,4,5,6,7,8,9,10];

var numberArray2 = [10,11,12,13,14,15,16,17,18,19];

x=0;
alert(x);
document.addEventListener('keydown', function (evt) {
if (evt.keyCode === 38) {
alert('The "UP" key is being held down...?');
alert(numberArray1);
x=x+1;
alert(x);
alert ("numberArray"+(x));
arrayGroup = "numberArray"+(x);
alert(arrayGroup);


}
if (evt.keyCode === 40) {
alert('The "DOWN" key is being held down...?');
arrayGroup = "numberArray"+(x-1);
x=x-1;
if(x<0){
x=0;
arrayGroup = "numberArray0";
}

alert(x);
alert(arrayGroup);
}
});

更新!!! - 我仍然遇到这个问题...

你好,

下面代码的目的是使用相对于从 numberArrays 获取的数据的 RGB 值来更新 SVG map 。每次用户使用光标时,我都想循环浏览不同的数组,这反过来又允许 map 改变颜色。目前我在使用全局变量 arrayGroup 时遇到困难,我无法使其余代码在按键时解释更新的数组。

如果您能提供任何建议,我们将不胜感激。

非常感谢!!!

var rsr = Raphael('map', '595.28', '841.89');

counties = [];

//obviously, this is the array storing the numbers of students for this particular year
numberArray0 = [46,54,38,125]:
//this empty array will store the different number categories i.e. 0-50, 51-100...
categories = [];

var arrayGroup = [];

numberArray1 = [460,200,384,135]:

numberArray2 = [260,100,584,335]:

x=0;

if(x===0){
arrayGroup = numberArray0;
alert(arrayGroup);}

document.addEventListener('keydown', function (evt) {
if (evt.keyCode === 38) {
alert('The "UP" key is being held down...?');
x+=1;
arrayGroup = eval ("numberArray"+(x));
alert(arrayGroup);


}
if (evt.keyCode === 40) {
alert('The "DOWN" key is being held down...?');
arrayGroup = eval ("numberArray"+(x-1));
x-=1;
alert(arrayGroup);
if(arrayGroup===numberArray+(-1)){
x=0;
alert(numberArray0);}
}
});

//this for loop populates the "categories" array with these categories, going up in fifties (i=0; i<61 becuase 60*50=3000, the number we were taking as the highest
for(i=0; i<61; i++){
if(i==0){
categories[0]=0;
}
else{
categories.push(categories[i-1]+50);
}
};

//empty array that will store the strings that define the colour of the counties
colourStrings=[];

//this loop goes through the population numbers for the county and finds out what category they are.
for(var i=0; i<arrayGroup.length; i++){
for(var j=0; j<categories.length; j++){
// this is a loop within a loop. First it takes a population number and runs through all the categories to see which one it's in
if(arrayGroup[i]>categories[j]){
//if it's bigger than the start point of the category(e.g. 71 is bigger than 50, goes in the 51-100 bracket) we take the index
index=j;
}


}
//the higher the category the population is in, the higher the index will be and the more the g and b values will be reduced
//the colour value wll then be stored in colourStrings at an index that corresponds with the county
colourStrings.push("rgb(255,"+(250-4*index).toString()+","+ (250-4*index).toString() +")");
}

最佳答案

您正在为变量 arrayGroup 分配一个字符串值,这就是您能够将它们作为字符串访问的原因。如果您想将它们作为数组访问,只需将所需的数组分配给 arrayGroup 变量,如下所示:

arrayGroup = numberArray1;
console.log(arrayGroup); // outputs: [1,2,3,4,5,6,7,8,9,10]

这样,arrayGroup[0]将输出1。您目前的做法是:

arrayGroup = "numberArray1";
console.log(arrayGroup[0]); // outputs: "n" because "n" is at index 0 of the string assigned to arrayGroup.

另外,关于增加和减少 x 值的一些建议:

x++; // increases the value of x by one
x += 1; // is the same as x = x + 1
x--; // decreases the value of x by one
x -= 1; // is the same as x = x -1

我认为这回答了你的问题(尽我所能理解)。如果这不是您想要的,请告诉我,我会看看是否可以提供进一步的帮助。我希望这有帮助!

关于javascript - 根据键码更改数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24854630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com