2013-04-27 3 views
0

사용자 제출에 의해 동적으로 채워지는 데이터베이스 테이블에서 문자열을 가져 오기 때문에 길이가 다양한 문자열 배열이 있습니다. 그러나 긴 배열은 반복되는 6 개의 변수로 구성됩니다. 그래서 매 6 번째 인덱스에서 슬라이스하여 배열로 썰어 놓은 다음 해당 하위 배열 목록을 페이지에 인쇄합니다.하나의 긴 배열에서 하위 배열 목록을 만들고 인쇄하는 방법

//I'm using Parse as my database and the find() method is one way you query it. 

var search = []; 

query.find({ 
    success: function(results) { 

//loop through results to get the key-value pair of each row 

    for (i = 0; i < results.length; i++){ 

    activity = results[i].get("activity"); 
    scene = results[i].get("location"); 
    neighborhood = results[i].get("neighborhood"); 
    date = results[i].get("date"); 
    details = results[i].get("details"); 
    time = results[i].get("time"); 

    //Here I'm pushing the row of the six variables into an array, but since there will be multiple rows on the table from multiple submissions, the array will contain however many rows there are. 

     search.push(activity, scene, neighborhood, date, details, time); 

    //my second array 

     var search2 = []; 

    //This is that part I'm unsure of. I want to loop through the search array slicing it at every 6th index creating multiple sub arrays. I then want to write the list of those sub arrays to the page 

    for(i=0; i < search.length; i+=6){ 
     search2 = search.slice(0,6); 
     } 

//this is the div I want to write the arrays to. 
    $("#mainDiv").text(search2); 

    };// closes success function 

답변

1

대신 검색 배열에 각 필드를 눌러 다음 각이 말에 설정 슬라이스의, 왜 그냥 같은 데이터의 각 레코드의 세트를 추가하지 않습니다 이것은 내가 지금까지 무엇을 가지고 배열을 검색 배열에 추가 하시겠습니까? 각 레코드의 값을 출력 할 수있는 검색 배열을 반복 당신이 이미 배열 슬라이스가 각 루프 6.에 의해 어떤 슬라이스 나 증가 할 필요가 없습니다 때

search.push([activity, scene, neighborhood, date, details, time]); 

그런 식으로, 당신이 원하는 :

for (i = 0; i < search.length; i++) { 
    $('#mainDiv').append(search[i]); 
} 

질문을 잘못 읽었을 수도 있지만 #mainDiv에 모든 레코드를 추가하려는 것 같습니다. 루프 반복마다 jQuery text() 메서드를 사용하면 각 반복마다 텍스트를 덮어 씀으로써 결과를 모든 항목의 모든 값 대신 루프의 최종 값으로 만듭니다. #mainDiv에 각 레코드를 추가하려면 append() 메소드를 사용하고 싶습니다.

당신은 또한 당신이 대신 개별 필드 배열 항목의 배열의 집합을 저장하는 나의 접근 방식을 사용하는 가정, 값 사이에 약간의 공간을 추가 할 수 있습니다 : 작동

for (i = 0; i < search.length; i++) { 
    $('#mainDiv').append(search[i].join(' ')); 
} 
+0

을! 감사. – Spilot

관련 문제