Change only one value in a deep object

I’m facing a problem with the manipulation of object. I want to change the value of a key, but I don’t know how to modificate this value without knowing the value of is parent object.

My exemple will speak for me.

My object look like that :

{day: {
   monday: {
      startTime: 2, 
      endTime: 1
  }
}}

I want to change the “startTime” value, I know I can do :

{day: {monday:{starTime: [value]}}}

But the problem is, that I don’t know the second key (it can be any day of the week). Like

{know: {
    unknow: {
        know: valueToChange
    }
}}

Do you know a trick for that ?

Answer

You can go over all values there and change each by iteration:

const data = {
  day: {
    monday: {
      startTime: 2, 
      endTime: 1
    },
    tuesday: {
      startTime: 2, 
      endTime: 1
    }
  }
}

Object.keys(data.day).forEach(key => {
  data.day[key].startTime = 10 // some known value
})

console.log(data)

This way you can change all of them or you can check if there is some specific key you want to change if you don’t want to change all of them