skip to Main Content

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


  1. const array = [
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object], [Object] ] },
      { nodes: [ [Object], [Object], [Object] ] },
      { nodes: [ [Object], [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] },
      { nodes: [ [Object], [Object], [Object] ] },
      { nodes: [ [Object], [Object] ] }
    ]
    
    const result = _.chain(array)
      .map(function(o) {
        return o.nodes
      })
      .value();
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
    Login or Signup to reply.
  2. You could use double map.

    First we loop through the outer array, for each array inside we return the value of nodes property.

    const before = [[
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
      { nodes: [ {}, {}, {} ] },
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
    ], [
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
      { nodes: [ {}, {}, {} ] },
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
    ]]
    
    const after = before.map(arr => {
        return arr.map(object => {
        return object.nodes
      })
    })
    
    console.log(after)

    You can also make it into a one-liner

    const after = before.map(arr => arr.map(object => object.nodes))
    
    Login or Signup to reply.
  3. From one of my above comments which does suggest a nested entirely map based solution …

    "Why at all is the OP looking for a lodash based solution when the language core offers a solution as simple as … array.map(nodeList => nodeList.map(({ nodes }) => nodes));?"

    const responseData = [[
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
      { nodes: [ {}, {}, {} ] },
    ], [
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {} ] },
      { nodes: [ {}, {}, {} ] },
    ]];
    
    console.log({ responseData });
    
    const sanitizedData = responseData
      .map(listOfNodesItems =>
        listOfNodesItems
          .map(({ nodes }) => nodes)
      );
    
    console.log({ sanitizedData });
    .as-console-wrapper { min-height: 100%!important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search