Jquery How to loop through JSON array and keep adding the value

Viewed 33
var jData = JSON.stringify(data)
                
  $.each(jData,function(key, value) {
       var usedAccu += jData[9][value];
     });

jData is Json array in the format like [{"key1": value, "key2": value, ... }, {..}].
I want to loop thru the array and keep adding key10's value into the variable called usedAccu.

1 Answers

You can try like below :

var usedAccu = 0
var jData = [{
  "key1": "1",
  "key2": "5",
}, {
  "key1": "1",
  "key2": "22",
}]
$.each(jData, function(key, value) {
  usedAccu += parseInt(value.key2) //change to your required keyname
});
console.log(usedAccu)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related