0

I Have the following json variable:

var test = {
    A: {
        A1: 1,
        A2: "Something",
        A3: 't'
    },
    B: "Something else",
    C: {
        C1: true
    }
}

now I have a variable "path" with a path to the value I want to change. Examples:

path = "A.A1",
path = "A.A3",
path = "B",
path = "C.C1".

From these values, I can split it into an array or whatever, but what I need afterwards is to access (and modify) the variable "test" from the path I got. Is there some generic way of doing that? Or my best alternative is to create a recursive function using the splitted array?

Ricardo Alves
  • 1,071
  • 18
  • 36
  • 5
    Possible duplicate of [Access object child properties using a dot notation string](https://stackoverflow.com/questions/8051975/access-object-child-properties-using-a-dot-notation-string) and [Accessing nested JavaScript objects with string key](https://stackoverflow.com/questions/6491463) and [Javascript: Get deep value from object by passing path to it as string](https://stackoverflow.com/questions/8817394) and [Access a nested property with a string](https://stackoverflow.com/questions/33066787) – adiga May 13 '19 at 13:58
  • `path.split('.').reduce((a, b) => a[b], test)` – adiga May 13 '19 at 14:01

0 Answers0