0

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:

  • use example from documentation

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

3 Answers 3

1

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"]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

You can use .flat to flatten the array.

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

1 Comment

Thanks for the suggestion, but with Ramda it should work in a different way. At list in my mind :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.