skip to Main Content

I would like to update the stautent status in array list. I would like update status with loop thought the arraylist.
How can I update the status in array list? Now I update all value when the 1st value is updated.

Now I have a list

[
{
            "studentId": 3,
            "result": 100,
            "resultStatus": false,
},
{
            "studentId": 2,
            "result": 20,
            "resultStatus": false,
},
{
            "studentId": 2,
            "result": 51,
            "resultStatus": false,
}
]


    
    const checkstatus = (result, passScore) => {
      if(result >= passScore){
        return true; //pass
      } else{
        return false; //fail
      } 
    }

    this.list =  this.list.map(x => ({
      ...x,
      resultStatus: checkstatus(x.result, 50 )
    }))
     

Expect result is

[
{
            "studentId": 3,
            "result": 100,
            "resultStatus": true,
},
{
            "studentId": 2,
            "result": 20,
            "resultStatus": false,
},
{
            "studentId": 2,
            "result": 51,
            "resultStatus": true,
}
]

2

Answers


  1. You can try the following way:

    const students = [
        {
            "studentId": 3,
            "result": 100,
            "resultStatus": false,
        },
        {
            "studentId": 2,
            "result": 20,
            "resultStatus": false,
        },
        {
            "studentId": 2,
            "result": 51,
            "resultStatus": false,
        }
    ];
    
    const passScore = 50;
    
    //use map() to create a new array with updated resultStatus values for each student
    const updatedStudents = students.map(student => ({
        ...student,
        //inline arrow function to check if the student's result is greater than or equal to the passScore
        resultStatus: ((result, passScore) => result >= passScore)(student.result, passScore)
    }));
    
    console.log(updatedStudents);
    Login or Signup to reply.
  2. I think you doesn’t need any function to calculate result status

    const students = [
        {
            "studentId": 3,
            "result": 100,
            "resultStatus": false,
        },
        {
            "studentId": 2,
            "result": 20,
            "resultStatus": false,
        },
        {
            "studentId": 2,
            "result": 51,
            "resultStatus": false,
        }
    ];
    
    const passScore = 50;
    
    
    const passed =  students.map(student=> ({...student,resultStatus:student.result >= passScore}))
    
    console.log(passed);
    .as-console-wrapper { max-height: 100% !important; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search