简体   繁体   English

更新for循环内的Javascript对象属性

[英]Update Javascript object property inside for loop

I am trying to update a JS object with another object which seems trivial, but the value is not updating. 我试图用另一个看似微不足道的对象来更新JS对象,但该值未更新。

 let sampleObj = { id: 1, name: 'Kelly' } let userData = [{ students: [{ id: 1, name: 'Sandra' }] }, { students: [{ id: 2, name: 'Jerome' }] } ] for (let group of userData) { for (let student of group.students) { if (student.id === sampleObj.id) { console.log('updating student object') student = sampleObj // student = { ...sampleObj } (another failed attempt) // userData[group].students[student] = sampleObj (another failed attempt) } } } console.log('userData', userData) 

It seems like, student is just some floating thing, unassociated to userData at the point where I'm trying to update its value. 看来, student只是一些浮动的东西,在我尝试更新其值时与userData无关。 However, I can't figure out how to make the update or what I'm missing. 但是,我不知道如何进行更新或缺少什么。

EDIT: The expected output is to replace the student object with sampleObj once its found. 编辑:预期的输出是找到后用sampleObj替换student对象。

Use forEach(el, index) instead so you have the index available to do the update: 使用forEach(el, index)代替,这样您就可以使用索引来进行更新:

 let sampleObj = { id: 1, name: 'Kelly' } let userData = [{ students: [{ id: 1, name: 'Sandra' }] }, { students: [{ id: 2, name: 'Jerome' }] } ] userData.forEach((group, m) => { group.students.forEach((student, n) => { if (student.id === sampleObj.id) { console.log('updating student object') userData[m].students[n] = sampleObj } }) }) console.log('userData', userData) 

Array item replacement does not work that way. 数组项替换不能以这种方式工作。 Instead you can replace the properties of student object like this 相反,您可以像这样替换student对象的属性

for (let group of userData) {
  for (let student of group.students) {
    if (student.id === sampleObj.id) {
      console.log('updating student object')
      student = Object.assign(student, sampleObj);
    }
  }
}

It'll assign all the properties of samplObj to student object. 它将把samplObj的所有属性分配给学生对象。

You're never updating the actual userData . 您永远不会更新实际的userData To update it you should reference the userData object in the loop, Not the temporary variable which is used to iterate the object. 要更新它,您应该在循环中引用userData对象,而不是用于迭代该对象的临时变量。

So try something like: 因此,尝试类似:

let sampleObj = { id: 1, name: 'Kelly' }

let userData = [
    {
    students: [
        { id: 1, name: 'Sandra' }
    ]
  },
  {
    students: [
        { id: 2, name: 'Jerome' }
    ]
  }
]
let j=0
for ( let group of userData ) {let i=0
    for ( let student of group.students ) { 
    if ( student.id === sampleObj.id ) {
        console.log('updating student object')
        userData[j].students[i] = sampleObj

      // student = { ...sampleObj }     (another failed attempt)
      // userData[group].students[student] = sampleObj     (another failed attempt)
    } i++;
  } j++;
}

console.log('userData', userData)

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

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