我正在尝试使用下面的代码使arrayGroup包含与按键向上或向下相关的数据集。根据它,它将在名为numberArray1的变量中添加或减去一个,并允许根据正在设置的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映射。每次用户使用光标时,我想循环遍历不同的数组,这反过来将允许地图改变颜色。目前我对全局变量arrayGroup有困难,我无法让其余的代码解释在keypress上更新的数组。
我们将非常感谢任何可以提供的建议。
非常感谢!!!
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() +")");
}
答案 0 :(得分:1)
您正在为变量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
我认为这可以回答你的问题(尽我所能)。如果这不是您正在寻找的内容,请告诉我,我会看看我是否可以提供进一步的帮助。我希望这有帮助!