I'm currently playing with D3.js.
While doing so I use this JSON-file as a data source: http://codepen.io/chriscoyier/pen/taqCe
Example of the structure:
{
"fathers" : [
{
"id" : 0,
"married" : false,
"name" : "Robert Wilson",
"sons" : null,
"daughters" : [
{
"age" : 21,
"name" : "Nancy"
},
{
"age" : 30,
"name" : "Amy"
}
]
},
{
"id" : 1,
"married" : true,
"name" : "Robert Thomas",
"sons" : null,
"daughters" : [
{
"age" : 22,
"name" : "Melissa"
},
{
"age" : 6,
"name" : "Susan"
}
]
},
...
]
}
I'm only interested in "daughters". I have mapped the objects into a flat array structure. The code I used:
var fathers = data.fathers,
separator = '-------------------',
persons = []; // collector array
// Filling the collector array with the
// desired data.
fathers.forEach(function(value) {
value['daughters'].forEach(function(value) {
persons.push(value);
});
});
It works fine. My complete code here: http://codepen.io/mizech/pen/WrNzJe
But I wonder: Aren't there better ways (in native JavaScript, no frameworks) then iterating?
Would be pleased if anyone has an idea and would share it.