简体   繁体   English

如何在 React js 中增加 Object 数组中的 Object?

[英]How to Increment the Object inside the Array, Of Array of Object in React js?

I want to increment the object inside the array students of array subjects.我想在数组科目的数组学生中增加 object。 And the state is as given below: state 如下所示:

  state = {
   students: [{
    name: "",
    address: "",
    class: "",
    subjects: [{ subjectName: "", duration: "" }],
  },
 ],
};

I write the Handle increment for subjects as below:我为主题编写了句柄增量,如下所示:

handleIncrement() {
this.state.students.map((student) => {
  this.setState({
    student: [...student.subjects, { subjectName: "", duration: "" }],
  });
});
console.log(this.state);
}

Button On Click to Increment as below:按钮点击增量如下:

<button
  type="button"
  className="btn btn-primary"
  style={myStyle.btnCircle}
  onClick={this.handleIncrement}>
      <i className="fa fa-plus"></i>
   </button>

Problem is I cannot increment the Object inside the subjects array.问题是我无法在主题数组中增加 Object 。 Please Can anyone help me to solve my problem on the above state format...Please do not suggest me to change my state, Data format.请任何人都可以帮我解决上述 state 格式的问题...请不要建议我更改我的 state 数据格式。 I'm using React js..我正在使用 React js..

Typo aside, I think you need to set the result of your map back to state, or you're going to be overwriting the state multiple times.除了打字错误,我认为您需要将 map 的结果设置回 state,否则您将多次覆盖 state。

handleIncrement() {
  this.setState({
    students: this.state.students.map((student) => ({
      ...student,
      subjects: [...student.subjects, { subjectName: "", duration: "" }]
    }))
  });
  console.log(this.state);
}

Note this will add a new entry to subjects for every student in the array, if you need it to be conditional you just need to change the map to only perform the change for the specific student, eg:请注意,这将为数组中的每个subjects添加一个新条目,如果您需要它是有条件的,您只需更改 map 以仅对特定学生执行更改,例如:

handleIncrement() {
  this.setState({
    students: this.state.students.map((student) => {
      if (student.name !== <name to update>) {
        return student;
      }
      return {
        ...student,
        subjects: [...student.subjects, { subjectName: "", duration: "" }]
      };
    })
  });
  console.log(this.state);
}
handleIncrement() {
   const newSubject = { subjectName: "", duration: "" }
   const newStudents = this.state.students.map((student) => 
                    { ... student, subjects: student.subjects.concat(newSubject) })
   this.setState({ students: newStudents })
}

I guess what you mean by increment is you want to push object(eg { subjectName: "2", duration: "2" }) into subjects array我猜你的意思是你想将对象(例如 { subjectName: "2", duration: "2" }) 推入主题数组

It is not good to call setState() in map() function.在map() function中调用setState()不好。 You need to create new state without mutating previous and assign it at once您需要创建新的 state 而不改变先前的并立即分配它

let newStudents = state.students.slice(0)
newStudents = newStudents.map(student => {
  student.subjects = [...student.subjects, { subjectName: "2", duration: "2" }]
  return student
})

Now you have desired students state in newStudents and can call setState()现在你在 newStudents 中有想要的学生state并且可以调用setState()

this.setState({students: newStudents})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM