2011-12-17 4 views
-1

데이터 제출 유용한 무언가로 다차원 배열을 변환 :I에 의해 생성되는 multidimentional 배열이

$('#order-submit').click(function(){ 
    var orderResult = []; 
    $(".dynamic-sale").each(function(){ 
     var individual_result = []; 
     $(this).find("select").each(function(){ 
      individual_result.push($(this).val()); 
     }) 
     orderResult.push(individual_result); 
    }); 

그것은 셔츠 판매의 동적 양식에서 데이터를 수집, 그리고처럼 보이는 배열 작성 를 [ 각 행에 대한 항목 당 크기, 수량 및 가격은 어느 것입니까 [ "3XL", "1", "15"], [ "XL", "1", "15]]. 총 비용을 계산할 수 있었지만 각 항목의 총 수량을 아약스 호출에 추가하여 데이터베이스에 제출할 수있는 형식으로 가져 오는 방법을 찾는 데 어려움을 겪고 있습니다. 내가 어떻게 그럴 수 있니?

감사합니다.

+2

? 서버가 데이터를 가져올 때 데이터가 어떻게 보이게해야합니까? – jyore

답변

2
// I added an element to your array for testing purposes 
var arr = [["3XL", "1", "15"], ["XL", "1", "15"], ["XL", "2", "15"]] 

// create an object to store each item 
var items = {} 

// loop over the array, building object with totals 
$.each(arr, function(i,v){ 
    // if the item is not already defined, then define it 
    if(typeof items[v[0]] == 'undefined') 
     // the `- 0` ensures that the value is treated as numeric 
     items[v[0]] = v[1] - 0 
    // else increment it 
    else 
     items[v[0]] += v[1] - 0 
}) 
+0

네, 정말 고마워요. 정확히 내가 무엇을 찾고 있었는지! – Bill

관련 문제