2011-12-17 61 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 2011-12-17 23:21:21

回答

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 2011-12-18 00:13:50