Introduction
In this lab, we will be exploring how to retrieve nested values from a JSON object based on an array of keys. We will be using the reduce() method to traverse the object's nested structure and retrieve the target value. By the end of this lab, you will have a better understanding of how to access specific values within a complex JSON object.
How to Retrieve a Nested Value in an Object Using an Array of Keys
To retrieve a specific value from a nested JSON object, you can use the deepGet function. This function takes in an object and an array of keys, and returns the target value if it exists in the object.
To use the deepGet function:
- Create an array of the keys you want to retrieve from the nested JSON object.
- Call the
deepGetfunction with the object and the array of keys. - The function will return the target value if it exists, or
nullif it does not.
Here is the code for the deepGet function:
const deepGet = (obj, keys) =>
keys.reduce(
(xs, x) => (xs && xs[x] !== null && xs[x] !== undefined ? xs[x] : null),
obj
);
And here is an example of how to use the deepGet function:
let index = 2;
const data = {
foo: {
foz: [1, 2, 3],
bar: {
baz: ["a", "b", "c"]
}
}
};
deepGet(data, ["foo", "foz", index]); // returns 3
deepGet(data, ["foo", "bar", "baz", 8, "foz"]); // returns null
To start practicing coding, open the Terminal/SSH and type node.
Summary
Congratulations! You have completed the Get Nested Value in Object Based on Array of Keys lab. You can practice more labs in LabEx to improve your skills.