0

If I have the state initialized like this:

this.state = {
        students: [
          {id: 0, grade: 'A'},
          {id: 513, grade: 'B'},
          {id: 429, grade: 'C'},
          {id: 172, grade: 'D'},
}

How would I be able to modify the fields of items?

This is what I have so far, but it's not working:

 updateGrade(id, grade) {
  const index = this.state.items.findIndex((x)=> x.id === id);
  if (index == -1) {
    console.log('invalid!');
  } else {
    this.setState({
      students: [
         ...this.state.students.slice(0, index),
         Object.assign({}, this.state.students[index], grade),
      ],
    });
  }
}

updateGrade(513, 'A');

0 Answers0