I have an object with nested properties. I want to update those nested properties by using an array of elements that reflect the nested sequence of the object. Also if a property does not exist, then add it, otherwise only update it.
Problem: is that I got all properties separated (non nested) with just empty objects ...
This is the function I tried implementing
function myFunction(myObject, ...myArrray) {
    let value = myArrray.pop()
    for (let i = 0; i < myArrray.length; i++) {
        const element = myArrray[i]
        if (!myObject.hasOwnProperty(element)) {
            if (i == myArrray.length)
                myObject[element] = value
            else
                myObject[element] = {}
        }
        else {
            myObject[element] = myObject[i + 1]
        }
    }
    return myObject
}
const myLocalStorage = {
    users: {
        theme: 'westeros',
        notifications: {
            email: true,
            push: {
                infos: true
            }
        }
    },
    admins: {
        // admin properties
    }
}
Updating "email" property (which exists)
const array1 = ["users", "notification", "email", false]
myFunction(myLocalStorage, array1)
- Adding "sms" property (which does not exist)
 
const array2 = ["users", "notification", "sms", true]
myFunction(myLocalStorage, array2)
    
setfunction does almost that (lodash.com/docs#set)._.set(myLocalStorage, "users.notification.email", false);