麻烦迭代一个对象

时间:2014-06-23 05:30:11

标签: javascript jquery arrays object

我有一个看起来像这样的对象:

salesDetails:{ "1":{
  "date":"06/22/2014",
  "amount":"45",
  "currency":"CAD",
  "productID":"23",
  "status":1},
"2":{
  "date":"06/22/2014",
  "amount":"120",
  "currency":"USD",
  "productID":"23",
  "status":1},
"3":{
  "date":"06/23/2014",
  "amount":"100",
  "currency":"USD",
  "productID":"21",
  "status":2},
"4":{
  "date":"06/23/2014",
  "amount":"250",
  "currency":"CAD",
  "productID":"25",
  "status":1},
"5":{
  "date":"06/23/2014",
  "amount":"180",
  "currency":"USD",
  "productID":"24",
  "status":1}
}

我想要做的是获得状态为“1”的所有货币的所有金额,并将其放在一个看起来像这样的对象中:

perCurrency: {
 "CAD":{
  "0":"45", 
  "1":"250"},

 "USD":{
  "0":"120",
  "1":"180"}
}

我能够将所有货币都放在一个对象中,但我遇到了金额问题,对象的最后一笔金额与之前的金额重叠。我一直在{"CAD":{"1":"250"},"USD":{"1":"180"}}这是我的代码到目前为止。

function countPerCurrency(){
  var currencyArray = new Array();
  var perCurrency = {}; 

  var totalSales = Object.size(salesDetails);

  for(var i=1; i <= totalSales; i++){
    var currency = salesDetails[i]["currency"];
    var amount = salesDetails[i]["amount"];
    var status = salesDetails[i]["status"];

    var totalCurrency = Object.size(currencyAmount[currency]);
    var currencyCtr = {};

    if(status == 1){
      if(!inArray(currency, currencyArray)){
        currencyArray.push(currency);
        currencyCtr[totalCurrency] = amount;
        perCurrency[currency] = currencyCtr;
      } else {
        var currencyAdd = {};
        currencyAdd[totalCurrency] = amount;
        perCurrency[currency] = currencyAdd;
      }
    }
  }         
}

我知道这看似简单,但我迷失在这里.. TIA! :)

4 个答案:

答案 0 :(得分:3)

之前接受的答案使用了一组值,而您要求一个对象。这是一个对象版本:

var perCurrency = {};
var currencyCount = {};

Object.keys(salesDetails).forEach(function(key) {
  var obj = salesDetails[key];
  var currency;

  if (obj.status == 1) {
    currency = obj.currency;

    // If this is first for this currency, add it to objects
    if (!currencyCount[currency]) {
      currencyCount[currency] = 0;
      perCurrency[currency] = {};
    }

    // Add currency values
    perCurrency[currency][currencyCount[currency]++] = obj.amount;
  }
}); 
BTW,这与jQuery无关。

请注意, Object.keys 是ES5,因此可能需要针对旧版浏览器进行填充,请参阅MDN:Object.keys了解代码。

答案 1 :(得分:0)

尝试这样的事情

    function countPerCurrency(){
          var perCurrency = {};
          var totalSales = Object.size(salesDetails);
          for(var i=1; i <= totalSales; i++){
            var currency = salesDetails[i]["currency"];
            var amount = salesDetails[i]["amount"];
            var status = salesDetails[i]["status"];

            if(status == '1'){
                if(perCurrency.hasOwnProperty(currency)){
                    // if currency already present than get currency array.
                    var currency_arr = perCurrency[currency];
                    // add new value to existing currency array.
                    currency_arr.push(amount);

                }else{
                    // if currency not present than create currency array and add first value;
                    var currency_arr = [];
                    currency_arr.push(amount);
                    // add newly created currency array to perCurrency object
                    perCurrency[currency] = currency_arr;

                }
            }
          }
          console.log(perCurrency);
    }

输出

    perCurrency: {
        "CAD":["45","250"],
        "USD":["120","180"],
    }

我创建了货币数组而不是键值对

答案 2 :(得分:0)

像这样更改你的代码,我简化了你的事情,

function countPerCurrency() {

   var perCurrency    = {}; 
       var currencyArray  = [];

   for(i in salesDetails)
   {
         if(!perCurrency.hasOwnProperty(salesDetails[i].currency) && salesDetails[i].status === "1")
               perCurrency[salesDetails[i].currency]     = [];


         if(salesDetails[i].status === "1")
         {
              currencyArray      = perCurrency[salesDetails[i].currency];
              currencyArray.push(salesDetails[i].amount);
              perCurrency[salesDetails[i].currency]     =    currencyArray;
         }   

   }     
}

答案 3 :(得分:0)

对于迟到的对话感到抱歉,请尝试使用.each()

var CAD = new Array();
var USD = new Array();

$.each(salesDetails, function (i, value) {

    if (value.status == 1) {

        if (value.currency == "CAD") {
            CAD.push(value.amount);

        } else if (value.currency == "USD") {
            USD.push(value.amount);

        }

    }

});
var perCurrency = {
    "CAD": CAD,
    "USD": USD
}

Demo