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:
For example:
var data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};
Preliminaries
In JavaScript there are only two (actually one) data types which can contain other data types: objects and arrays (a special form of objects).
Both types expose akey -> value
structure. Keys in
arrays must be numeric, whereas any string can be used as key in
objects. The key-value pairs are also called the "properties".
Properties can be accessed either using dot notationor bracket notation, if the property name would not be a valid JavaScript identifier name [spec], or the name is the value of a variable:var value = obj.someProperty;
For the same reason, array elements can only be accessed using bracket notation:// the space is not a valid character in identifier names
var value = obj["some Property"];
// property name as variable
var name = "some Property";
var value = obj[name];
var value = arr[5];
// property name / index as variable
var x = 5;
var value = arr[x];Wait... what about JSON?
JSON is a textual representation of data, just like XML, YAML, CSV, and others. To work with such data, it first has to be converted to JavaScript data types, i.e. arrays and objects (and how to work with those was just explained). How to parse JSON is explained in the question How to parse JSON in JavaScript .
Accessing nested data structures
A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.
Here is an example:
Lets assume we want to access thevar data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};name
of the second item.
Here is how we can do it step-by-step:
As we can seedata
is an object, hence we can access its properties using dot notation. Theitems
property is accessed as follows:
The value is an array, to access its second element, we have to use bracket notation:data.items
This value is an object and we use dot notation again to access thedata.items[1]
name
property. So we eventually get:
var item_name = data.items[1].name;
What if the property names are dynamic and I don't know them beforehand?
If the property names are unknown or we want to access all properties of an object / elements of an array, we can use thefor...in
[MDN] loop for objects and thefor
[MDN] loop for arrays to iterate over all properties / elements.
To iterate over all properties ofdata
, we can iterate over the object like so:
To iterate over all objects infor(var prop in data) {
// `prop` contains the name of each property, i.e. `'code'` or `'items'`
// consequently, `data[prop]` refers to the value of each property, i.e.
// either `42` or the array
}data.items
array, we use afor
loop:
One could also usefor(var i = 0, l = data.items.length; i < l; i++) {
// `i` will take on the values `0`, `1`, `2`,..., i.e. in each iteration
// we can access the next element in the array with `data.items[i]`, example:
//
// var obj = data.items[i];
//
// Since each element is an object (in our example),
// we can now access the objects properties with `obj.id` and `obj.name`.
// We could also use `data.items[i].id`.
}for...in
to iterate over arrays, but there are reasons why this should be avoided: Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?.
Depending on where the object comes from (and what you want to do), you might have to test in each iteration whether the property is really a property of the object, or it is an inherited property. You can do this withObject#hasOwnProperty
[MDN].
What if the "depth" of the data structure is unknown to me?
In addition to unknown keys, the "depth" of the data structure (i.e. how many nested objects / array) it has, might be unknown as well. How to access deeply nested properties depends on the exact data structure then.
If the data structure contains repeating structures, e.g. the representation of a binary tree, the solution typically includes to recursively [Wikipedia] access each level of the data structure. Here is an example to get the first leaf node of a binary tree:
DEMOfunction getLeaf(node) {
if (node.leftChild) {
return getLeaf(node.leftChild); // <- recursive call
}
else if (node.rightChild) {
return getLeaf(node.rightChild); // <- recursive call
}
else { // node must be a leaf node
return node;
}
}
var first_leaf = getLeaf(root);
A more generic way to access a nested data structure with unknown keys and depth is to test the type of the value and act accordingly. Here is an example which adds all primitive values inside a nested data structure into an array (assuming it does not contain any functions)
DEMOfunction toArray(obj) {
var result = [];
for (var prop in obj) {
var value = obj[prop];
if (typeof value === 'object') {
result.push(toArray(value)); // <- recursive call
}
else {
result.push(value);
}
}
return result;
}
Helpers
Since the structure of a complex object or array is not necessarily obvious, we can inspect the value at each step to decide how to move further.console.log
[MDN] andconsole.dir
[MDN] help us doing this. For example (output of the Chrome console):
Here we see that that> console.log(data.items)
[ Object, Object ]data.items
is an array with two elements which are both objects. In Chrome console the objects can even be expanded and inspected immediately.
This tells us that> console.log(data.items[1])
Object
id: 2
name: "bar"
__proto__: Objectdata.items[1]
is an object and after expanding it we see that it has three properties,id
,name
and__proto__
. The latter is an internal property used for the prototype chain of the object. The prototype chain and inheritance is out of scope for this answer though.
Further reading material
How to access arrays and objects is basic JavaScript knowledge and therefore it is advisable to read the MDN JavaScript Guide, especially the sections
0 comments:
Post a Comment