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.
-
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 withsampleObj
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
3 Answers
Reset to default 8Use 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)