2011-12-17 18 views
-1

を提出するのに便利なものに多次元配列を変換します[ :それはシャツの売り上げのダイナミックなフォームからデータを収集し、ように見える配列を作成します私はによって生成された多次元配列持つデータ

$('#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"] 各行のアイテムあたりのサイズ、数量、価格はどれですか。私は総コストを計算することができましたが、各アイテムの合計数をajax呼び出しに追加してデータベースに送信できる形式にする方法を考え出すのは苦労しています。どうすればいい?

ありがとうございます!

+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