So I have this array of objects, which respectively contain other objects:
const template_columns = [
{
"id": 38,
"name": "Ideas",
"position": 0,
"template_id": 4,
"column_type": "Column",
"workflow": [
{
"action": "Next",
"move_to": "Backlog",
"assign_to": "none"
},
{
"action": "Prev",
"move_to": "First column",
"assign_to": "none"
}
],
"_t": "template_column"
},
{
"id": 39,
"name": "Backlog",
"position": 1,
"template_id": 4,
"column_type": "Column",
"workflow": [
{
"action": "Start",
"move_to": "Current",
"assign_to": "none"
}
],
"_t": "template_column"
},
{
"id": 40,
"name": "Current",
"position": 2,
"template_id": 4,
"column_type": "Column",
"workflow": [
{
"action": "Review",
"move_to": "Review",
"assign_to": "none"
}
],
"_t": "template_column"
},
{
"id": 41,
"name": "Review",
"position": 3,
"template_id": 4,
"column_type": "Column",
"workflow": [
{
"action": "Approve",
"move_to": "Approved",
"assign_to": "none"
}
],
"_t": "template_column"
},
{
"id": 42,
"name": "Approved",
"position": 4,
"template_id": 4,
"column_type": "Columns::Done",
"workflow": [],
"_t": "template_column"
}
]
I need to process this array and return an array with all workflow.action
values. I have loadash available and I'm also transpiling the es6 code with babel.
I managed to do so, but I'm wondering if there's a more elegant way to do it:
My solution:
const workflow = _.map (
_.flatten(
_.map(template_columns, function(col) {
return col.workflow
})
), 'action'
)
So is there a better way to achieve what I'm looking for?
var res = []; template_columns.forEach(function(column) { res.push(...column.workflow.map(action => action.action)); });
\$\endgroup\$ – Tushar Dec 15 '16 at 5:34