Suat Atan PhD
Suat Atan PhD

Reputation: 1382

Sum the values with underscore.js

I have JSON data like below.

{  
   "market":[  
      {  
         "aciklama":"Migros",
         "kategori":"market",
         "odeme_tipi":"kredi_karti",
         "tarih":{  
            "seconds":1557673429,
            "nanoseconds":599000000
         },
         "tutar":"10",
         "user_email":"[email protected]",
         "user_uid":"6xx"
      },
      {  
         "aciklama":"Pidesi",
         "kategori":"market",
         "odeme_tipi":"nakit",
         "tarih":{  
            "seconds":1557173938,
            "nanoseconds":955000000
         },
         "tutar":"5",
         "user_email":"[email protected]",
         "user_uid":"6xx"
      }
   ],
   "diger":[  
      {  
         "aciklama":"Tatlı",
         "kategori":"diger",
         "odeme_tipi":"nakit",
         "tarih":{  
            "seconds":1557591963,
            "nanoseconds":180000000
         },
         "tutar":"20",
         "user_email":"[email protected]",
         "user_uid":"6xx"
      }
   ]
}

I want to get the sum of subnodes of market, diger via tutar property and want to extract this result with underscore.js:

market: 15 
diger: 20

I tried reduce function of undersore.js but I could not. What is the best way to extract child nodes and process it?

Upvotes: 0

Views: 142

Answers (2)

jannis
jannis

Reputation: 5220

You can do it with pure JS OFC, but here's a requested underscore.js approach using reduce

const input = {
  "market": [{
    "aciklama": "Migros",
    "kategori": "market",
    "odeme_tipi": "kredi_karti",
    "tarih": {
      "seconds": 1557673429,
      "nanoseconds": 599000000
    },
    "tutar": "10",
    "user_email": "[email protected]",
    "user_uid": "6xx"
  }, {
    "aciklama": "Pidesi",
    "kategori": "market",
    "odeme_tipi": "nakit",
    "tarih": {
      "seconds": 1557173938,
      "nanoseconds": 955000000
    },
    "tutar": "5",
    "user_email": "[email protected]",
    "user_uid": "6xx"
  }],
  "diger": [{
    "aciklama": "Tatlı",
    "kategori": "diger",
    "odeme_tipi": "nakit",
    "tarih": {
      "seconds": 1557591963,
      "nanoseconds": 180000000
    },
    "tutar": "20",
    "user_email": "[email protected]",
    "user_uid": "6xx"
  }]
};

var output = _.reduce(_.pairs(input), (memo, kv) => {
  const key = kv[0];
  memo[key] = _.reduce(kv[1],
    (memo1, o) => memo1 + parseInt(o.tutar),
    0);
  return memo;
}, {});

console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

Upvotes: 1

HRK44
HRK44

Reputation: 2772

I'm not sure why you want to use a library for that... Pure JS :

let res = {};
for(let category in myObj) {
     res[category] = 0;

     for(let sub of myObj[category]) {
        res[category] += parseInt(sub.tutar, 10);             
     }
}
return res;

Or if you really want to use a library, this is with lodash which has a sum function https://lodash.com/docs/4.17.11#sumBy :

let res = {};
for(let category in myObj) {
     res[category] = _.sumBy(myObj[category], o => parseInt(o.tutar, 10));
}
return res;

Or with underscore js reduce :

let res = {};
for(let category in myObj) {
     res[category] = _.reduce(myObj[category], (memo, sub) => { return memo + parseInt(sub.tutar, 10); }, 0);
}
return res;

Upvotes: 1

Related Questions