最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Update Javascript object property inside for loop - Stack Overflow

programmeradmin4浏览0评论

I am trying to update a JS object with another object which seems trivial, but the value is not updating.

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)

I am trying to update a JS object with another object which seems trivial, but the value is not updating.

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. 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.

Share Improve this question edited Jan 18, 2019 at 0:36 bruh asked Jan 18, 2019 at 0:31 bruhbruh 2,3058 gold badges34 silver badges44 bronze badges 8
  • What exactly is the expected output? Do you want to change the found student's name to the new one of the sampleObj? – CertainPerformance Commented Jan 18, 2019 at 0:32
  • I would like to replace the entire student object with sampleObj once its found – bruh Commented Jan 18, 2019 at 0:33
  • 1 the var student only lives inside the inner for loop, so UserData is not affected from the assignment. – Jeff Commented Jan 18, 2019 at 0:34
  • You need to replace the object at the same index in group.students. – Heretic Monkey Commented Jan 18, 2019 at 0:35
  • 1 I'd suggest - as connexo does, in his answer. - that you should use an Array.prototype.forEach() that way you have access to the array-element, the index and the array itself. – David Thomas Commented Jan 18, 2019 at 0:41
 |  Show 3 more ments

3 Answers 3

Reset to default 8

Use forEach(el, index) instead so you have the index available to do the update:

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

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.

You're never updating the actual userData. To update it you should reference the userData object in the loop, Not the temporary variable which is used to iterate the object.

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)
发布评论

评论列表(0)

  1. 暂无评论