Accessing and editing nested object keys in JavaScript through path

64 Views Asked by At

I want to access and edit a **nested ** key in the object (not the value but the key itself).

eg;

{
  "name": {
    "firstname": "Boba",
    "lastName": "Fett"
  }
}

For example I want to edit the name.firstname field to FullName as shown below;

{
  "name": {
    "FullName": "Boba",
    "lastName": "Fett"
  }
}

Using lodash, tried unsetting the key value pair and setting a new pair with the key changed but this will add the new pair to the end of the json, i want to preserve the order of the json.

Also note that the key may be nested several levels deep and i have the path to the key. Are there any other libraries or other ways i can do this instead of using a recursive loop?

Im using JavaScript.

Thanks in advance:)

0

There are 0 best solutions below