0

I have the json data in array.


    var obj = [
        {
            student_data:{
                name: 'brj',
                id: '123',
                marks:[{'hi': 12, 'mt': 20, 'en': 20}]
            }
        },
        {
            student_data:{
                name: 'anand',
                id: '124',
                marks:[{'hi': 12, 'mt': 20, 'en': 20}]
            }
    
        }
    ]

here I want to add the marks and display the total, but i cant get it right because I'm unable to get the addition, here is the solution i tried.


    var i = 0;
    var t = 0;
    for(var i = 0; i<obj.length; i++){
        for(var j = 0; j<obj[i].student_data.marks[0].length; j++){
            t += obj[i].student_data.marks[0];
            //console.log(obj[i].student_data.marks[0])
            //console.log(t);
    
        };
        
    }

Expected output should be,

{ '123':{ name: 'bji', total_marks: 52 } }, { '124':{ name: 'anand', total_marks: 52 } }

Somesh
  • 1
  • 1

3 Answers3

1

obj[i].student_data.marks[0] is an object, you can't add it to a number.

You need to iterate through its values, which can be done using Object.values().

var obj = [{
    student_data: {
      name: 'brj',
      id: '123',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }
  },
  {
    student_data: {
      name: 'anand',
      id: '124',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }
  }
];

var t = 0;
obj.forEach(o => o.student_data.marks.forEach(marks =>
  Object.values(marks).forEach(mark => t += mark)));
console.log(t);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks and how do i replace student data with their repective ids. – Somesh Jul 14 '21 at 19:29
  • Not sure what you mean by that. This is totalling the marks of all students, not doing a per-student total. – Barmar Jul 14 '21 at 19:31
  • i mean to say in our data there is a object name student_data i want to replace student_data with their respective ids. – Somesh Jul 14 '21 at 19:34
  • example: student_data: { name: 'brj', id: '123', marks: [{ 'hi': 12, 'mt': 20, 'en': 20 } here student_data should be changed to '123' as it has id 123, it should look like, { '123':{ name: 'bji', total_marks: 52 } } – Somesh Jul 14 '21 at 19:36
  • I think he means he wants the 'student_data' key to be replaced with the numerical value of the 'student_data' 'id' value. So the first one would change from 'student_data' to '123' – MCC Jul 14 '21 at 19:39
  • @Somesh That's a completely different question. – Barmar Jul 14 '21 at 19:39
  • yes correct. student_data name should be replaced with "id" – Somesh Jul 14 '21 at 19:41
  • yes i know sorry for the inconvenience, but is there any way i would be able to do this. – Somesh Jul 14 '21 at 19:42
  • Yes, of course there is. `o[student_data.id] = what you want` – Barmar Jul 14 '21 at 19:47
  • I'll leave doing `what you want` as your own exercise, I'm not going to do your homework for you. – Barmar Jul 14 '21 at 19:48
0

You can use forEach to iterate over the appropriate arrays:

var obj = [{
    student_data: {
      name: 'brj',
      id: '123',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }
  },
  {
    student_data: {
      name: 'anand',
      id: '124',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }

  }
]
var sum = 0;
obj.forEach(e => e.student_data.marks.forEach(f => Object.keys(f).forEach(g => sum += f[g])));
console.log(sum);
Spectric
  • 30,714
  • 6
  • 20
  • 43
0

Although the two above are more efficient, you may wish to see it presented similar to your current code. Given your desired output:

var total = 0;
for(var i = 0; i < obj.length; i++){
    obj[i].student_data["total_marks"] = 0;
    for(var k in obj[i].student_data.marks[0]){
        obj[i].student_data["total_marks"] += obj[i].student_data.marks[0][k];
    };    
}
console.log(obj);
MCC
  • 512
  • 1
  • 5
  • 23
  • https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea?lq=1 – Barmar Jul 14 '21 at 19:32