Currently tackling a React project issue regarding object merging with the setState method. Two objects are involved:
person1 = {
name: 'ramya',
age: 21,
gender: 'f',
skills:{
technicalSkills: {
languages: ['C', 'C++', 'Java'],
operatingSystems: ['Unix', 'Linux']
},
linguisticSkills: {
toSpeak: ['English', 'Tamil'],
toRead: ['English']
}
},
}
obj2 = {
skills: {
linguisticSkills: {
toRead: ['English', 'Tamil']
}
}
}
The objective is to update person1's skills.linguisticSkills to
linguisticSkills: {
toSpeak: ['English', 'Tamil'],
toRead: ['English', 'Tamil']
}
But given changes in skills.linguisticSkills.toSpeak
and skills.linguisticSkills.toRead
, how can I effectively merge person1 and obj2 in setState for the desired outcome?
UPDATE: _.merge struggles with cases like this:
person1 = {
name: 'ramya',
age: 21,
gender: 'f',
skills:{
technicalSkills: {
languages: ['C', 'C++', 'Java'],
operatingSystems: ['Unix', 'Linux']
},
linguisticSkills: {
toSpeak: ['English', 'Tamil'],
toRead: ['English', 'Tamil', 'Telugu']
}
},
}
obj2 = {
skills: {
linguisticSkills: {
toRead: ['English', 'Tamil']
}
}
}
resulting in the same state as person 1 instead of the expected:
{
name: 'ramya',
age: 21,
gender: 'f',
skills:{
technicalSkills: {
languages: ['C', 'C++', 'Java'],
operatingSystems: ['Unix', 'Linux']
},
linguisticSkills: {
toSpeak: ['English', 'Tamil'],
toRead: ['English', 'Tamil']
}
},
}