2014-07-20 5 views
0

아래 코드를 사용하여 키 입력과 관련된 데이터 집합을 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); 
    } 
}); 

업데이트 !!! - 난 아직도

안녕하세요, 아래 코드의

목적은 RGB가 numberArrays에서 가져온 데이터를 기준으로 값이있는 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()  +")"); 
} 

답변

1

문자열 값을 문자열로 액세스 할 수있는 변수 arrayGroup에 문자열 값을 할당합니다.

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

이 방법 arrayGroup[0] 출력됩니다 1을 : 당신은이 같은 arrayGroup 변수에 원하는 배열을 할당 배열로 액세스하십시오. 현재 그 일을하는 방법은 다음과 같습니다 또한

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 

이 귀하의 질문에 (최고로 내가 이해할 수로 응답 그것). 이것이 당신이 찾고있는 것이 아닌지 알려 주시면 더 도와 드릴 수 있는지 보겠습니다. 이 도움이 되었길 바래!

+0

귀하의 의견에 감사 드리며 그 내용을 기록했지만, 제 문제는 좀 더 복잡하다고 생각합니다. 기회가 생기면 내 업데이트 된 코드를 확인하십시오. 다시 한 번 감사드립니다 ... – owlwink