jQuery对象:对值A的所有实例求和,其中值B为重复

时间:2015-10-13 10:04:33

标签: javascript jquery object knockout.js

我有VAT值的这个对象:

[
{
 "code": "2",
 "rate": "17.5%",
 "net": 0
},
{
 "code": "2",
 "rate": "17.5%",
 "net": 0
},
{
 "code": "1",
 "rate": "Zero Rated",
 "net": 0
},
{
 "code": "1",
 "rate": "Zero Rated",
 "net": 0
},
{
 "code": "4",
 "rate": "20%",
 "net": 304.7
},
{
 "code": "1",
 "rate": "Zero Rated",
 "net": 0
},
{
 "code": "4",
 "rate": "20%",
 "net": 1200.24
},
{
 "code": "4",
 "rate": "20%",
 "net": 5201.04
},
{
 "code": "4",
 "rate": "20%",
 "net": 1258.82
},
{
 "code": "4",
 "rate": "20%",
 "net": 629.41
},
{
 "code": "4",
 "rate": "20%",
 "net": 629.41
}
]

我想要做的是显示所有单独的代码及其各自的值总结,即我想将增值税代码为1的所有净值相加并显示它(代码,费率和总净值),然后单独显示他们的增值税代码是2,等等。

我尝试过使用$ .each,创建一个新对象并比较两者,然后添加值或创建一个新的实例,其中代码相同,但它变得混乱和混乱:

var vatAnalysisItem = [];

$.each(self.vat(), function(index) {
  $.each(self.vat()[index], function(key1, value1) {
    if(key1 == "code") {
      if(vatAnalysisItem.length === 0) {
        vatAnalysisItem.push({code: self.vat().code, rate: self.vat().rate, net: self.vat().net});
      }
      else {
        $.each(vatAnalysisItem, function(key2, value2) {
          if(key2 == "code")
            if(value in vatAnalysisItem) {
              vatAnalysisItem[net] += self.vat().net;
            }
            else {
              vatAnalysisItem.push({code: self.vat()[code], rate: self.vat().rate, net: self.vat().net});
            }
        });
      }
    }
  });
});

。我不知道该怎么做。我将使用Knockout JS在View中显示完整的值。

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

您可以使用带有临时对象的简单循环,例如

var items = [{
  "code": "2",
  "rate": "17.5%",
  "net": 0
}, {
  "code": "2",
  "rate": "17.5%",
  "net": 0
}, {
  "code": "1",
  "rate": "Zero Rated",
  "net": 0
}, {
  "code": "1",
  "rate": "Zero Rated",
  "net": 0
}, {
  "code": "4",
  "rate": "20%",
  "net": 304.7
}, {
  "code": "1",
  "rate": "Zero Rated",
  "net": 0
}, {
  "code": "4",
  "rate": "20%",
  "net": 1200.24
}, {
  "code": "4",
  "rate": "20%",
  "net": 5201.04
}, {
  "code": "4",
  "rate": "20%",
  "net": 1258.82
}, {
  "code": "4",
  "rate": "20%",
  "net": 629.41
}, {
  "code": "4",
  "rate": "20%",
  "net": 629.41
}];

var result = [],
  tmp = {};
$.each(items, function(i, item) {
  if (!tmp[item.code]) {
    tmp[item.code] = {
      "code": item.code,
      "rate": item.rate,
      "net": 0
    };
    result.push(tmp[item.code]);
  }
  console.log(tmp[item.code])
  tmp[item.code].net += item.net;
});

output.innerHTML = JSON.stringify(result, null, 2)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="output"></pre>

答案 1 :(得分:1)

排序顺序可能不对,请根据口味调整。

var vm = {
  data: ko.observableArray([]),
  cumulativeData: ko.pureComputed(function() {
    var result = {};
    vm.data().forEach(function(datum) {
      var cumul = result[datum.code];
      if (!cumul) {
        result[datum.code] = cumul = {
          code: datum.code,
          rate: datum.rate,
          net: 0
        };
      }
      cumul.net += datum.net;
    });
    return Object.keys(result).sort().map(function(code) { return result[code]; });
  })
};

ko.applyBindings(vm);

var newData = [{"code":"2","rate":"17.5%","net":0},{"code":"2","rate":"17.5%","net":0},{"code":"1","rate":"Zero Rated","net":0},{"code":"1","rate":"Zero Rated","net":0},{"code":"4","rate":"20%","net":304.7},{"code":"1","rate":"Zero Rated","net":0},{"code":"4","rate":"20%","net":1200.24},{"code":"4","rate":"20%","net":5201.04},{"code":"4","rate":"20%","net":1258.82},{"code":"4","rate":"20%","net":629.41},{"code":"4","rate":"20%","net":629.41}];
vm.data(newData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<table data-bind="foreach: cumulativeData">
  <tr>
    <td data-bind="text: code"></td>
    <td data-bind="text: rate"></td>
    <td data-bind="text: net"></td>
  </tr>
</table>

答案 2 :(得分:0)

您可以将代码用作新对象中的键,以便轻松获取所需信息:

var out = arr.reduce(function (p, c) {
  var key = c.code;
    if (!p[key]) p[key] = { rate: c.rate, net: 0}
    p[key].net += c.net;
    return p;
}, {});

out['4'].net // 9223.619999999999

<强>输出

{
    "1": {
        "rate": "Zero Rated",
        "net": 0
    },
    "2": {
        "rate": "17.5%",
        "net": 0
    },
    "4": {
        "rate": "20%",
        "net": 9223.619999999999
    }
}

DEMO