Javascript 我在for循环中得到相同的对象,以便在角度上进行修改

Javascript 我在for循环中得到相同的对象,以便在角度上进行修改,javascript,arrays,angular,object,scope,Javascript,Arrays,Angular,Object,Scope,所以我想执行一个循环,它本质上修改了迭代中元素的结构。但是,对于修改,我有一个预定义的结构对象,我已经将其设置为默认对象。现在,当我在对象数组(需要修改的数组)的每个元素上循环时,我将默认对象和当前元素一起发送给其他函数,该函数本质上返回一个修改过的对象。但是,我传递的默认对象在每次迭代后都会被修改。如何阻止修改默认对象 例如,我有我想要修改的数据 [{ "name": "Sample 1", "session_id": "sample_id_1", "created":

所以我想执行一个循环,它本质上修改了迭代中元素的结构。但是,对于修改,我有一个预定义的结构对象,我已经将其设置为默认对象。现在,当我在对象数组(需要修改的数组)的每个元素上循环时,我将默认对象和当前元素一起发送给其他函数,该函数本质上返回一个修改过的对象。但是,我传递的默认对象在每次迭代后都会被修改。如何阻止修改默认对象

例如,我有我想要修改的数据

[{
    "name": "Sample 1",
    "session_id": "sample_id_1",
    "created": "2020-04-14T06:45:05.494Z",
    "isUploaded": false,
    "username": "username1",
    "topics": [
        {
            "name": "2",
            "isUploaded": true,
            "topic_id": "2"
        },
        {
            "name": "3",
            "isUploaded": true,
            "topic_id": "3"
        }
    ]
},
{
    "name": "Sample_2",
    "session_id": "sample_id_2",
    "created": "2020-04-14T05:43:10.643Z",
    "isUploaded": false,
    "username": "username2",
    "topics": [
        {
            "name": "2",
            "isUploaded": true,
            "topic_id": "2"
        },
        {
            "name": "1",
            "isUploaded": true,
            "topic_id": "1"
        },
        {
            "name": "4",
            "isUploaded": true,
            "topic_id": "4"
        }
    ]
}]
我要使用的默认对象数组是:

[
    {
      topic_id: '1',
      topic_status: '-1',
      topic_name:
      {
        en: 'Some english text 1?',
        hi: 'some hindi text 1?'
      },
      isPlayed: false,
      status: false
    }, {
      topic_id: '2',
      topic_status: '-1',
      topic_name: {
        en: 'some english text 2?',
        hi: 'some hindi text 2?'
      },
      isPlayed: false,
      status: false
    },
    {
      topic_id: '3',
      topic_status: '-1',
      topic_name: {
        en: 'some english text 3?',
        hi: 'some hindi text 3?'
      },
      isPlayed: false,
      status: false
    },
    {
      topic_id: '4',
      topic_status: '-1',
      topic_name: {
        en: 'some english text 4?',
        hi: 'some hindi text 4?'
      },
      isPlayed: false,
      status: false
    }
  ]
现在我已经编写了这个循环,它将根据默认对象修改循环中的每个元素,如下所示:

async getServerSessionsAsLocal(serverSessions) { // serverSessions contains the sample Array of Objects described above
const newArray = [];
const defaultQuestionsForLocal = await this.userService.getUserQuestions(); // this will return the default object Array described above
if (defaultQuestionsForLocal) {
  for (let i = 0 ; i < serverSessions.length; ++i) {
    const generatedSession = this.syncService.generateSession(serverSessions[i], [...defaultQuestionsForLocal]); // this function essentially modifies the serverSession object according to the default object Array
    newArray.push(generatedSession);
  }
}
return [...newArray];
}

现在我面临的问题是,当我将defaultQuestionsArray传递给generateSession函数时,它会在内部修改这个defaultQuestionsArray,将其分配回会话对象的topics键,并返回完整的对象。对于第一次迭代,一切看起来都很好,但是对于任何后续迭代,defaultQuestions数组都会被修改。因此,在第二次迭代之后,defaultQuestions数组的每个元素中的topic_status键将根据索引2处的示例对象进行修改。 例如,在第二次迭代之后,defaultQuestionsArray看起来像:

[
{
  topic_id: '1',
  topic_status: '3',
  topic_name:
  {
    en: 'Some english text 1?',
    hi: 'some hindi text 1?'
  },
  isPlayed: false,
  status: false
}, {
  topic_id: '2',
  topic_status: '3',
  topic_name: {
    en: 'some english text 2?',
    hi: 'some hindi text 2?'
  },
  isPlayed: false,
  status: false
},
{
  topic_id: '3',
  topic_status: '3',
  topic_name: {
    en: 'some english text 3?',
    hi: 'some hindi text 3?'
  },
  isPlayed: false,
  status: false
},
{
  topic_id: '4',
  topic_status: '3',
  topic_name: {
    en: 'some english text 4?',
    hi: 'some hindi text 4?'
  },
  isPlayed: false,
  status: false
}
[{
 "name": "Sample 1",
 "session_id": "sample_id_1",
 "created": "2020-04-14T06:45:05.494Z",
 "isUploaded": false,
 "username": "username1",
 "topics": [
 {
    "name": "1",
    "isUploaded": false,
    "topic_id": "1",
    "topic_status": 3
 },
 {
    "name": "2",
    "isUploaded": true,
    "topic_id": "2",
    "topic_status":3,
 },
 {
    "name": "3",
    "isUploaded": true,
    "topic_id": "3",
    "topic_status": 3,
 },
 {
    "name": "4",
    "isUploaded": false,
    "topic_id": "4",
    "topic_status": 3,
 }
]
}, {
"name": "Sample_2",
"session_id": "sample_id_2",
"created": "2020-04-14T05:43:10.643Z",
"isUploaded": false,
"username": "username2",
"topics": [
    {
        "name": "1",
        "isUploaded": true,
        "topic_id": "1",
        "topic_status": 3
    },
    {
        "name": "2",
        "isUploaded": true,
        "topic_id": "2",
        "topic_status": 3
    },
    {
        "name": "1",
        "isUploaded": true,
        "topic_id": "1",
        "topic_status": 3
    },
    {
        "name": "4",
        "isUploaded": true,
        "topic_id": "4",
        "topic_status": 3
    }
]
}]
]

请注意,第三个索引对象在第一次迭代中被修改时被设置为3,其余的在当前迭代中被修改。由于我将这个结果对象分配给提供的会话对象的topics键,所以前面修改过的对象的topics键也会得到相同的值。这意味着,在第二次迭代之后,新数组将如下所示:

[
{
  topic_id: '1',
  topic_status: '3',
  topic_name:
  {
    en: 'Some english text 1?',
    hi: 'some hindi text 1?'
  },
  isPlayed: false,
  status: false
}, {
  topic_id: '2',
  topic_status: '3',
  topic_name: {
    en: 'some english text 2?',
    hi: 'some hindi text 2?'
  },
  isPlayed: false,
  status: false
},
{
  topic_id: '3',
  topic_status: '3',
  topic_name: {
    en: 'some english text 3?',
    hi: 'some hindi text 3?'
  },
  isPlayed: false,
  status: false
},
{
  topic_id: '4',
  topic_status: '3',
  topic_name: {
    en: 'some english text 4?',
    hi: 'some hindi text 4?'
  },
  isPlayed: false,
  status: false
}
[{
 "name": "Sample 1",
 "session_id": "sample_id_1",
 "created": "2020-04-14T06:45:05.494Z",
 "isUploaded": false,
 "username": "username1",
 "topics": [
 {
    "name": "1",
    "isUploaded": false,
    "topic_id": "1",
    "topic_status": 3
 },
 {
    "name": "2",
    "isUploaded": true,
    "topic_id": "2",
    "topic_status":3,
 },
 {
    "name": "3",
    "isUploaded": true,
    "topic_id": "3",
    "topic_status": 3,
 },
 {
    "name": "4",
    "isUploaded": false,
    "topic_id": "4",
    "topic_status": 3,
 }
]
}, {
"name": "Sample_2",
"session_id": "sample_id_2",
"created": "2020-04-14T05:43:10.643Z",
"isUploaded": false,
"username": "username2",
"topics": [
    {
        "name": "1",
        "isUploaded": true,
        "topic_id": "1",
        "topic_status": 3
    },
    {
        "name": "2",
        "isUploaded": true,
        "topic_id": "2",
        "topic_status": 3
    },
    {
        "name": "1",
        "isUploaded": true,
        "topic_id": "1",
        "topic_status": 3
    },
    {
        "name": "4",
        "isUploaded": true,
        "topic_id": "4",
        "topic_status": 3
    }
]
}]
我想要的是,每次我将defaultQuestionsArray传递给generate session函数时,它都应该是默认数组。有没有关于如何纠正这种情况的想法?

使用:

Object.create(originalTemplateObject);
如果您遇到修改原始对象的问题,此方法将返回对象及其属性的副本,而不保留引用,ref:

然后可以修改副本,而不是修改通过引用传递的原始副本

例如:

因此,您的默认对象模板如下所示:

{
  topic_id: '1',
  topic_status: '-1',
  topic_name:
  {
    en: 'Some english text 1?',
    hi: 'some hindi text 1?'
  },
  isPlayed: false,
  status: false
}
您应该将此模板保存在所有代码之外,因为它应该是一个全局常量:

const objectTemplate = {
  topic_id: '',
  topic_status: '',
  topic_name:
  {
    en: '',
    hi: ''
  },
  isPlayed: false,
  status: false
}
这里我们有一个函数,它创建对象,然后使用传递的参数返回新对象

const objectTemplate={
主题id:“”,
主题“”状态:“”,
主题名称:
{
嗯,
您好:''
},
显示:错误,
状态:false
}
常量myObjects=[];
函数copyObject(…objectValues){
设i=0;
const newObject=Object.create(objectTemplate);
for(让道具插入新对象){
newObject[prop]=objectValues[i];
i++;
}
返回newObject;
}
肌对象推(
copyObject('1','-1',{en:'Some english text 1?',hi:'Some hindi text 1?},false,false
));
log(“具有对象副本的数组:“+”\n”);
console.log(myObjects);
console.log(“\n”+”原始对象:“+”\n”);

log(objectTemplate)感谢@Riven为您提供答案。我也可以通过使用扩展操作符来解决这个问题。毫无疑问,Object.create在这里也可以用。