2012-11-30 7 views
0

내 응용 프로그램에서 다음 전역 변수는 시작 :자바 스크립트 추가 배열

events = [];

을 그리고 난 다음과 같은 간단한 코드로 아약스 뭔가를 가져 가면 :

events[0] = []; 

setTimeout(function fetchEventsThisWeek(){ 

    $.ajax({ 
     url: '/event/getEventsBetweenDates', 
     type: 'POST', 
     data : { from_date : currentweek.from_date.toString("yyyy-MM-d") , to_date : currentweek.to_date.toString("yyyy-MM-d"), limit : limit, offset : offset }, 
     success: function(data) { 
      jQuery.each(data, function(index){ 
       events[0].push(data[index]['attributes']); 
      }); 




      offset = offset + limit; 
      entry_count = entry_count + data.length; 

      if(data.length < limit) { // We reached the end of the table so we don't need to call the function again 
       renderEvents(current, offset - limit, entry_count); 
       //Make sure the current next week button gets enabled because we are done with the results 

      } else { 
       renderEvents(current, offset - limit, offset); 
       setTimeout(fetchEventsThisWeek, 150); // There are more results, continue 
      } 

     } 
    }) 
}, 150); 

이 재귀 함수는 두 날짜 사이의 모든 이벤트를 가져오고 왼쪽에 레코드가 없을 때까지 자신을 계속 호출합니다. 내 문제는 다음과 같습니다 변수로

:

events[0] = []; 

내가 내 주 항목으로 배열의 인덱스를 지정합니다. 따라서 특정 주를 찾으면 배열 인덱스에 의해 이미 배열에서 가져온 모든 항목을 가져올 수 있습니다.

내가 더 주를 가져 오기 할 때 내 문제는, 그래서 예를 들면 :

events[1] = [];// Index 1 would represent the next week 

배열은 크기가 확장하고 모든 끝에 추가됩니다, 그래서 하나 개의 큰 배열이 아닌 다차원 왜 이런거야? 그리고 어떻게이 행동을 얻을 수 있습니까?

편집 : 내 질문에 확장하겠습니다.

events 변수에 여러 json 객체 배열이 필요합니다. 그래서 ..

events[0] = [ /*contains array of json objects */]; 
events[1] = [ /*contains array of json objects */]; 
events[2] = [ /*contains array of json objects */]; 

각 배열 색인은 1 주를 나타냅니다. 따라서 색인 0은 현재 주, 색인 1은 1 주, 색인 2는 2 주 등입니다. 난 다음 작업을 수행하고 싶지만이 가능하다조차 모르겠어요 :

events[-1] = [ /*contains array of json objects */]; 

은 어디 지수는 -1 과거에 일주 할 것이다. 이게 가능하면 아무도 알려주지 못했을까요?

+0

나는이 문제를 이해하지 않습니다. 하드 코딩 된'0' 인덱스를 매개 변수화하기를 원합니까? 그리고, 그런데'setTimeout'은 무엇을하고 있습니까? – bfavaretto

답변

1

당신은 Array.unshift 찾고 :

events.unshift([]); 

Documentation