遍历地图打字稿

时间:2020-03-07 02:18:16

标签: angular typescript

我有一个名为Saveck(aid: number)

的函数
Saveck(aid: number) {
    const modelsToChange: any[] = [];
    const model: any = {};
    for (const value of this.submittedCheckAnswers.values()) {
          model.AssignmentId = assignmentId;
          model.QaId = value.qaId;
          model.Action = 'Insert';
          console.log(model);
          modelsToChange.push(model);
    }
    console.log(modelsToChange);
}

submittedCheckAnswers是在 进入此for循环之前正确初始化的地图。

console.log(this.submittedCheckAnswers) will give the following result:

Map(2) {2 => {…}, 3 => {…}}
  [[Entries]]
    0: {2 => Object}
      key: 2
      value:
        answerId: 5
        answerContext: "9"
        aOrdinal: 2
        isAnswer: false
        ngAnswerId: "Answer2"
        ngAnswerField: ""
        qaId: 56
        __proto__: Object
    1: {3 => Object}
      key: 3
      value:
        answerId: 6
        answerContext: "8"
        aOrdinal: 3
        isAnswer: false
        ngAnswerId: "Answer3"
        ngAnswerField: ""
        qaId: 57

for循环内的命令console.log(model);给出以下结果: //第一次

{AssignmentId: 1, QaId: 56, Action: "Insert"}
  AssignmentId: 1
  QaId: 57
  Action: "Insert"

//第二次

{AssignmentId: 1, QaId: 57, Action: "Insert"}
  AssignmentId: 1
  QaId: 57
  Action: "Insert"

否,我第一次复制和粘贴对象时没有在 QaId 中打错字。出于某些奇怪的原因,对象在{AssignmentId: 1, QaId: 56, Action: "Insert"}QaId: 56这样的控制台上打印,但是当我展开对象

AssignmentId: 1
QaId: 57
Action: "Insert"

QaId是57 !!!!我似乎不明白为什么?这57被插入到模型中并附加到modelsToChange上。因此,modelsToChange是错误的。

有人可以向我解释发生了什么事吗?

0 个答案:

没有答案