嵌套" for"相反嵌套数据的循环

时间:2016-10-04 21:12:14

标签: javascript for-loop data-structures

我很抱歉我的问题令人困惑。不确定我需要的是什么。

我的目标不是多次点击我的数据库。所以,我试图从我已经拥有的变量中获得一切可能。

我的数据结构类似于以下内容:

[$i]

依旧......

我需要得到以下内容:

reportData = [
    {
        date: '08/01/16',
        data: [
            {
                product: 'orange',
                picked: 20,
                washed: 15,
                sold: 11
            },{
                product: 'lemon',
                picked: 45,
                washed: 38,
                sold: 22
            },{
                product: 'apple',
                picked: 36,
                washed: 33,
                sold: 29
            }
        ]
    },{
        date: '08/02/16',
        data: [
            {
                product: 'orange',
                picked: 53,
                washed: 45,
                sold: 41
            },{
                product: 'lemon',
                picked: 44,
                washed: 31,
                sold: 21
            },{
                product: 'apple',
                picked: 76,
                washed: 55,
                sold: 45
            }
        ]
    }
]

totalPicksByDate = [
    {
         date: '08/01/06',
         picked: 101 //sum of all the picked products
     },{
         date: '08/02/16',
         picked: 173
     }
]

对于totalPicksByDate,我有一个嵌套的" for"循环(这部分很简单):

totalPicksByProduct = [
    {
         product: 'orange',
         picked: 73 //sum of all the picked oranges
     },{
         product: 'lemon',
         picked: 99
     },{
         product: 'apple',
         picked: 112
     }
]

我的问题:从这个数据结构中获取 totalPicksByProduct 是一种很好的智能有效方法(它就像嵌套"用于"循环,但内部)或我应该再次访问我的数据库并重组reportData变量?或许我完全错了......

2 个答案:

答案 0 :(得分:0)

您可以使用循环尝试此代码段:



var reportData = [{
    date: "08/01/16",
    data: [{
      product: "orange",
      picked: 20,
      washed: 15,
      sold: 11
    }, {
      product: "lemon",
      picked: 45,
      washed: 38,
      sold: 22
    }, {
      product: "apple",
      picked: 36,
      washed: 33,
      sold: 29
    }]
  }, {
    date: "08/02/16",
    data: [{
      product: "orange",
      picked: 53,
      washed: 45,
      sold: 41
    }, {
      product: "lemon",
      picked: 44,
      washed: 31,
      sold: 21
    }, {
      product: "apple",
      picked: 76,
      washed: 55,
      sold: 45
    }]
  }],
  totalPicksByDate = [],
  totalPicksByProduct = [],
  l = reportData.length,
  report, item, j, data, sum, k, temp, found;
while (report = reportData[--l]) { //note assignment
  data = report.data;
  j = data.length;
  sum = 0;//sum of all picked items by date
  while (item = data[--j]) {
    sum += item.picked;
    k = totalPicksByProduct.length;
    found = false;
    while (temp = totalPicksByProduct[--k]) { //here we pick items by product name
      if (found = (temp.product === item.product)) {
        temp.picked += item.picked;//found in list, so update
        break;
      }
    }
    if (!found) {//item isn't in the list, so add new
      totalPicksByProduct.unshift({
        'product': item.product,
        'picked': item.picked
      });
    }
  }
  
  totalPicksByDate.unshift({
    'date': report.date,
    'picked': sum
  });
}
document.write('<pre>totalPicksByDate=' + JSON.stringify(totalPicksByDate, 0, 4) +'<br/>totalPicksByProduct='+ JSON.stringify(totalPicksByProduct, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

您也可以尝试使用Array.forEach():

&#13;
&#13;
var reportData = [{
    date: "08/01/16",
    data: [{
      product: "orange",
      picked: 20,
      washed: 15,
      sold: 11
    }, {
      product: "lemon",
      picked: 45,
      washed: 38,
      sold: 22
    }, {
      product: "apple",
      picked: 36,
      washed: 33,
      sold: 29
    }]
  }, {
    date: "08/02/16",
    data: [{
      product: "orange",
      picked: 53,
      washed: 45,
      sold: 41
    }, {
      product: "lemon",
      picked: 44,
      washed: 31,
      sold: 21
    }, {
      product: "apple",
      picked: 76,
      washed: 55,
      sold: 45
    }]
  }],
  totalPicksByDate = [],
  totalPicksByProduct = [],
  sumByDate, foundByProduct;
reportData.forEach(function(report) {
  sumByDate = 0;
  report.data.forEach(function(data) {
    sumByDate += data.picked;
    foundByProduct = false;
    totalPicksByProduct.forEach(function(temp) {
      if (temp.product == data.product) {
        temp.picked += data.picked;
        foundByProduct = true;
      }
    });
    if (!foundByProduct) {
      totalPicksByProduct.push({
        'product': data.product,
        'picked': data.picked
      });
    }
  });
  totalPicksByDate.push({
    'date': report.date,
    'picked': sumByDate
  });
});
document.write('<pre>totalPicksByDate=' + JSON.stringify(totalPicksByDate, 0, 4) +'<br/>totalPicksByProduct='+ JSON.stringify(totalPicksByProduct, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

totalPicksByDate可以如下计算:

    var totalPicksByDate = $.map(reportData, function (item, index) {
        var total = { date: item.date, picked: 0 };
        $.each(item.data, function (idx, product) {
            total.picked += product.picked;
        });
        return total;
    });

totalPicksByProduct稍微复杂一些:

    var totalPicksByProduct = [];
    var temp = {};
    $.each(reportData, function (index, item) {
        $.each(item.data, function (idx, product) {
            temp[product.product] = temp[product.product] || 0;
            temp[product.product] += product.picked;
        });
    });
    for(var name in temp)
    {
        totalPicksByProduct.push({ product: name, picked: temp[name] });
    }