1

i have data from json this data json

var someObject = {
 "exam": 
 [
   {
     "re": [[{"result": "3"}],[{"result": "1"}],[{"result": "2"}]]
   },
   {
     "re": [[{"result": "4"}],[{"result": "4"}],[{"result": "4"}]]
   }
   ,
   {
     "re": [[{"result": "2"}],[{"result": "1"}],[{"result": "3"}]]
   }
 ]
};

and the problem is how to sum the result?

and I've tried in code like this

for (var i = 0; i<someObject.exam.length; i++){
    for (var y = 0; y < someObject.exam[i].re.length; y++){

         $scope.RESS = parseInt(someObject.exam[i].re[y][0].result) +
                       parseInt(someObject.exam[i].re[y][0].result) 
  }
}

why the results not same like

 3 + 4 + 2; // 9
 1 + 4 + 1; // 6
 2 + 4 + 3; // 9

this xample from plunk xamplle

I've spent 2 days for something like this, and now I'm tired help me pleasse

2
  • you want the output as above? Commented Nov 16, 2017 at 16:13
  • @Sajeetharan yes how, can help me Commented Nov 16, 2017 at 16:15

1 Answer 1

2

This is how you should calculate it:

someObject.exam[0].re.forEach(function(o, i) {
  results.push(parseInt(someObject.exam[0].re[i][0].result) + parseInt(someObject.exam[1].re[i][0].result) + parseInt(someObject.exam[2].re[i][0].result));
});

Demo:

var someObject = {
  "exam": [{
      "re": [
        [{
          "result": "3"
        }],
        [{
          "result": "1"
        }],
        [{
          "result": "2"
        }]
      ]
    },
    {
      "re": [
        [{
          "result": "4"
        }],
        [{
          "result": "4"
        }],
        [{
          "result": "4"
        }]
      ]
    },
    {
      "re": [
        [{
          "result": "2"
        }],
        [{
          "result": "1"
        }],
        [{
          "result": "3"
        }]
      ]
    }
  ]
};

var results = [];

someObject.exam[0].re.forEach(function(o, i) {
  results.push(parseInt(someObject.exam[0].re[i][0].result) + parseInt(someObject.exam[1].re[i][0].result) + parseInt(someObject.exam[2].re[i][0].result));
});
console.log(results);

Sign up to request clarification or add additional context in comments.

2 Comments

what the hell is this man, you know 2 days i'm stuck with this code and you finish it in a few minutes, niceeee you save my day, I really thanks to you
@AdriyanaPutraPratama Hhhhh you are welcome, It just needs some practice and you will be able to perform better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.