Check if object has a new property on loop

    var test = [{name:"A",result:23},{name:"A",result:54},{name:"A",result:23},{name:"A",result:32},
                                   {name:"B",result:26},{name:"B",result:34},{name:"B",result:13},{name:"B",result:42}];
   var student = [];
   var count = [];
  var total = 0;
  var _result = 0;
  var _name = "";

  for (var i = 0; i < test.length; i++) {
      
      _name = test[i].name; 
      _result = test[i].result;
   
    //here i want to check for new property entries on student object 
    //if a new property is assigned, let the property be pushed into the student 
     //object, else continue the iteration and sum up the result of  the existing 
      //property.
     
     //the problem i am having is that when the "B" property is assigned, the    
    // does not show, making the total to keep counting.
   // Please i need help on this, its my first time. 
  //( maybe am using the wrong condition to check for new properties) :/

      if(typeof student._name === "undefined" ||  typeof count._name=== "undefined" ){
         alert(" new property");
          student._name = 0;
           count._name = 0;
           _result += student._name;
              total = count._name;
              
               student.push({_name : _result});
                count.push({_name : total});
           
                                     }else{
           _result += student._name;
            total = count._name;
                                  }
          student._name = _result;
           count._name = total + 1;
           
           alert(_result);
      }
1 Like

Student shouldn’t be an array but an object, right?
Otherwise, student._name has no meaning…

I am not an expert by any means but the logic seems to me unconvincing:
Let’s say you run a test and add the results for A and B in your test object.
This process is repeated every time you run a test. But then, if there is a typo in A or B names’ spelling, this will mess up all the process…ain’t it?

I’d go for a walk, think about something simpler (seems your coding skills are fine) and try to implement some debugging strategy (console.logs at the very least).

Oops! Ya… You are right, but mu student will be array of objects like… [{A : result}, {B : result}] or better of {A: result, B:result}, thats my aim tho, Thanks let .me go through it again.

Not sure if this is close you your aim:


const test = [{name:"A",result:23},{name:"B",result:54},{name:"A",result:23},{name:"B",result:32},
                                   {name:"A",result:26},{name:"B",result:34},{name:"A",result:13},{name:"B",result:42}];
//this is all students in a test
let student = { };
let _result = 0;
let _name = "";

for (let i = 0; i < test.length; i++) {
_name = test[i].name;
_result = test[i].result;
if(!student[_name]){
console.log(" new property");
student[_name]=_result;
}
//if it already exists, we add the result to the previous result in student's object.
else {
   student[_name]+= _result;
   console.log(student[_name]);
}
console.log(student)
}

The output is:


 new property
{ A: 23 }
 new property
{ A: 23, B: 54 }
46
{ A: 46, B: 54 }
86
{ A: 46, B: 86 }
72
{ A: 72, B: 86 }
120
{ A: 72, B: 120 }
85
{ A: 85, B: 120 }
162
{ A: 85, B: 162 }
1 Like

Yea kinda close… My aim is actually this
At the end of the loop student will be:
// sumReuslt = sum of result for each //property
{A : sumResult, B : sumResult}
{A :132, B : 115}

°

var results = [
  { name: "A", result: 23 },
  { name: "A", result: 54 },
  { name: "A", result: 23 },
  { name: "A", result: 32 },
  { name: "B", result: 26 },
  { name: "B", result: 34 },
  { name: "B", result: 13 },
  { name: "B", result: 42 }
];

var students = results.reduce((studentObj, { name, result }) => {
  studentObj[name] = studentObj.hasOwnProperty(name)
    ? studentObj[name] + result
    : result;
  return studentObj;
}, {});

console.log(students); // { A :132, B : 115 }

OR

var students = results.reduce((studentsObj, { name, result }) => {
  var studentObj = studentsObj.hasOwnProperty(name)
    ? { [name]: studentsObj[name] + result }
    : { [name]: result };
  return { ...studentsObj, ...studentObj };
}, {});

console.log(students); // { A :132, B : 115 }
1 Like

my results are not those just because I tweaked the numbers, if that’s what you mean.

Omg! this solution might have looked easy for you… but you just saved me!
go ahead merry and drink for this you will make HAVEN! Thanks so much!
thanks guys!