I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?
For example:
var data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};
How could I access the name
of the second item in items
?
Objects and arrays has a lot of built-in methods that can help you with processing data.
Note: in many of the examples I'm using arrow functions. They are similar to function expressions, but they bind the
this
value lexically.Object.keys()
,Object.values()
(ES 2017) andObject.entries()
(ES 2017)Object.keys()
returns an array of object's keys,Object.values()
returns an array of object's values, andObject.entries()
returns an array of object's keys and corresponding values in a format[key, value]
.Object.entries()
with a for-of loop and destructuring assignmentIt's very convenient to iterate the result of
Object.entries()
with a for-of loop and destructuring assignment.For-of loop lets you iterate array elements. The syntax is
for (const element of array)
(we can replaceconst
withvar
orlet
, but it's better to useconst
if we don't intend to modifyelement
).Destructuring assignment lets you extract values from an array or an object and assign them to variables. In this case
const [key, value]
means that instead of assigning the[key, value]
array toelement
, we assign the first element of that array tokey
and the second element tovalue
. It is equivalent to this:As you can see, destructuring makes this a lot simpler.
Array.prototype.every()
andArray.prototype.some()
The
every()
method returnstrue
if the specified callback function returnstrue
for every element of the array. Thesome()
method returnstrue
if the specified callback function returnstrue
for some (at least one) element.Array.prototype.find()
andArray.prototype.filter()
The
find()
methods returns the first element which satisfies the provided callback function. Thefilter()
method returns an array of all elements which satisfies the provided callback function.Array.prototype.map()
The
map()
method returns an array with the results of calling a provided callback function on the array elements.Array.prototype.reduce()
The
reduce()
method reduces an array to a single value by calling the provided callback function with two elements.The
reduce()
method takes an optional second parameter, which is the initial value. This is useful when the array on which you callreduce()
can has zero or one elements. For example, if we wanted to create a functionsum()
which takes an array as an argument and returns the sum of all elements, we could write it like that:Using JSONPath would be one of the most flexible solutions if you are willing to include a library: https://github.com/s3u/JSONPath (node and browser)
For your use case the json path would be:
so:
Using lodash would be good solution
Ex:
If you are looking for one or more objects that meets certain criteria you have a few options using query-js
There's also a
single
and asingleOrDefault
they work much likefirst
andfirstOrDefault
respectively. The only difference is that they will throw if more than one match is found.for further explanation of query-js you can start with this post
A pythonic, recursive and functional approach to unravel arbitrary JSON trees:
where data is a python list (parsed from a JSON text string):
You can access it this way
or
Both ways are equal.