Chains and Flattened Arrays
Using Lodash, we can flatten complex array structures into a single array, and then remove duplicates. Chains simplify the whole process.
HTML
<h3>Flattened Actions</h3>
<ul id="flattened"></ul>
JavaScript
var data = [
{
name: "Item 1",
actions: [ {id: "Create"}, {id: "Read"}, {id: "Delete"} ]
},
{
name: "Item 3",
actions: [ {id: "Delete"} ]
}
];
// Four things happen here:
// 1. Build the chainable data object.
// 2. Build the flattend array.
// 3. Remove deplicate values.
// 4. Assign resulting object to "actions".
var actions = _.chain( data )
.flatten( "actions" )
.uniq();
var actions2 = _.flatten(data, "actions");
console.log("actions2 ", actions2);
console.log("actions ", actions);
console.log("find ", _.find(actions, {id: "Delete"}))
//console.log("actions ", actions)
// The DOM action list.
var list = document.getElementById( "flattened" );
// The chain object is iterable.
actions.each( function( action ) {
var item = document.createElement( "li" );
item.appendChild( document.createTextNode( action ) );
list.appendChild( item );
});