Andrew Losseff
Andrew Losseff

Reputation: 373

Ramda concat array issue

How to concat two arrays in Ramda.js?

I have next data:

const inputData = {
  content: [
    {

      info: ['Test-1-1', 'test-1-2'],

      moreInfo: ['foo', 'bar'],

      firstName: 'first',

      lastName: 'lst',

      notes: 'Some info goes here'

    },
        {

      info: ['Test-2-1', 'test-2-2'],

      moreInfo: ['foo-2', 'bar-2'],

      firstName: 'first',

      lastName: 'lst',

      notes: 'Some info goes here-2'

    },
  ]
}

I can manipulate this data just fine, but I cannot combine two arrays into one.

What I need to do, is to take

info: ['Test-2-1', 'test-2-2'],
moreInfo: ['foo-2', 'bar-2'],

End return

"theInfo": ["Test-1-1", "test-1-2", "foo", "bar"]

My code:

const allInfo = (R.props(['info', 'moreInfo']));

const returnNewObject = R.applySpec({

 // More code here to do other stuff

  theInfo: allInfo,

})

R.map(returnNewObject, inputData.content)

What I get:

{
  // other info

  "theInfo": [["Test-1-1", "test-1-2"], ["foo", "bar"]]
}

What I've tried:

R.concat([4, 5, 6], [1, 2, 3]);

But it returns array of empty object, for some reasons it doesn't work like in the documentation

Upvotes: 0

Views: 96

Answers (3)

Scott Sauyet
Scott Sauyet

Reputation: 50797

You can certainly write something like this (with an additional field combination for demo purposes)L

const extract = applySpec ({
  theInfo: compose (unnest, props (['info', 'moreInfo'])),
  fullName: compose (join (' '), props (['firstName', 'lastName'])),
  notes: prop ('notes')
})

const process = evolve ({
  content: map (extract)
}) 

const inputData = {content: [{info: ['Test-1-1', 'test-1-2'], moreInfo: ['foo', 'bar'], firstName: 'first', lastName: 'lst', notes: 'Some info goes here'}, {info: ['Test-2-1', 'test-2-2'], moreInfo: ['foo-2', 'bar-2'], firstName: 'first', lastName: 'lst', notes: 'Some info goes here-2'}]}

console .log (process (inputData))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script> const {applySpec, compose, unnest, props, join, prop, evolve, map} = R </script>

And if we had no other use for it, we could embed extract inside process like this:

const process = evolve ({
  content: map (applySpec ({
    theInfo: compose (unnest, props (['info', 'moreInfo'])),
    fullName: compose (join (' '), props (['firstName', 'lastName'])),
    notes: prop('notes')
  }))
})

But there is a good question of how much Ramda adds to a using a vanilla JS version:

const process = ({content, ...rest}) => ({
  ...rest,
  content: content.map (({info, moreInfo, firstName, lastName, notes}) => ({
    theInfo: info .concat (moreInfo),
    fullName: `${firstName} ${lastName}`,
    notes
  }))
})

If I was already using Ramda in my project, I would choose the slightly more declarative Ramda version; it is a close call, though. And I certainly wouldn't add Ramda just for this.

Upvotes: 1

Andrew Losseff
Andrew Losseff

Reputation: 373

The answer is:

const allInfo = R.compose(R.flatten, (R.props(['info', 'moreInfo'])))

End it returns flatten array:

["Test-1-1", "test-1-2", "foo", "bar"]

Upvotes: 1

M.Hassan Nasir
M.Hassan Nasir

Reputation: 851

You can use .flat to flatten the array.

theInfo.flat() //["Test-1-1", "test-1-2", "foo", "bar"]

Upvotes: 0

Related Questions