I’m using lodash to transform a response from a GraphQL. What I’ve is the following structure:
[[
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {}, {} ] },
{ nodes: [ {}, {}, {} ] },
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {}, {} ] },
], [
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {}, {} ] },
{ nodes: [ {}, {}, {} ] },
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {} ] },
{ nodes: [ {}, {}, {} ] },
]]
But what I need is the following structure:
[[
[ {}, {} ],
[ {}, {} ],
[ {}, {}, {} ],
[ {}, {}, {} ],
[ {}, {} ],
[ {}, {}, {} ],
], [
[ {}, {} ],
[ {}, {}, {} ],
[ {}, {}, {} ],
[ {}, {} ],
[ {}, {} ],
[ {}, {}, {} ],
]]
What I’ve is the following chain:
_.chain(resp.data.data.projects.nodes)
.flatMap('ciVariables')
.value()
Any idea how to get the target structure using the chain in Lodash? I already tried to use flatMap('ciVariables.nodes')
, but then all objects are in the top array.
3
Answers
You could use double map.
First we loop through the outer array, for each array inside we return the value of nodes property.
You can also make it into a one-liner
From one of my above comments which does suggest a nested entirely
map
based solution …