I am currently working on dynamically modifying an object based on a given path, but I am encountering some difficulties in the process. I have managed to create a method that retrieves values at a specified path, and now I need to update values at that path. The length of the path should not be a limiting factor, so using prop.prop.prop.prop
should still work. For instance:
let dataObj = {
name: 'Old Name',
address: {
state: 'CA',
timezone: 'America/California'
}
}
let modifiedValues = {
'name': 'New Name'
'address.state': 'FL',
'address.timezone': 'America/New_York'
}
My goal is to iterate over the second object and utilize the key-value pairs to either update the first object directly or construct a new object with the same structure. Ultimately, I aim to achieve an object as follows:
let dataObj = {
name: 'New Name',
address: {
state: 'FL',
timezone: 'America/New_York'
}
}
Additionally, below is the method I currently have for retrieving a value based on a provided path:
getAttributeFromPath(path: string, entity: any): any {
return path.split('.').reduce((a, b) => a && a[b], entity);
}