I want to use lodash
get method to return data in a nested object. The problem is what I'm using can either be a single string, or a string with a not notation representing a nested object.
An example of the data:
data: {
animals: {
dogs: {
labrador: {
age: 1
},
dalmation: {
age 5
},
},
lion: {
age: 28
}
}
}
To access the lion's age I could use get(data, ["animals", "lion", "age"], []);
But let's say I get a string const string = "dogs.labrador"
. I wouldn't be able to just swap out lion
for string
in the get method.
Is there an easy way to access the labrador's age?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/1BLizxI
Comments
Post a Comment