-1

Hi first of all I would like to state that yes, this question has been answered here Update value of a nested dictionary of varying depth however, this is in Python.

I would like to know if there's a Javascript approach to solving this issue.

Let's say I have a dictionary as such:

{
  "group1" : {
    "name" : "NAME1"
  },
  "group2" : {
    "name" : "NAME2"
  }
}

How would I be able to replace "NAME1" with lets say "Bob White" without doing this dict["group1"]["name"] = "Bob White"?

Could someone help please? Thanks

SunAwtCanvas
  • 1,261
  • 1
  • 13
  • 38

1 Answers1

0

You can loop through each property and set the value of its name property:

const obj = {
  "group1" : {
    "name" : "NAME1"
  },
  "group2" : {
    "name" : "NAME2"
  }
}

Object.keys(obj).forEach(e => obj[e].name = "Bob White")

console.log(obj)
Spectric
  • 30,714
  • 6
  • 20
  • 43