These are chat archives for ramda/ramda

15th
Apr 2018
Mike Lambert
@lax4mike
Apr 15 2018 01:38
R.identity
David
@DavidBernal
Apr 15 2018 15:04
Hi, I have the next list:
[{user: 'a', foo: 'bar'}, {user: 'a', foo: 'buz'}]
I want convert to:
[{user: 'a', foo: ['bar', 'buz']}]
what is the best function for that?
Mike Lambert
@lax4mike
Apr 15 2018 15:09
hmm, maybe R.mergeWith or R.mergeWithKey
David
@DavidBernal
Apr 15 2018 15:12
thanks, i will try
Mike Lambert
@lax4mike
Apr 15 2018 15:14
i wonder if R.groupBy could be used...
you'll also probably need to use them with R.reduce if you want to do this for the whole list
this is a tricky one though...
const users = [
  { user: "a", foo: "bar" },
  { user: "a", foo: "buz" },
  { user: "b", foo: "bar" }
]

const reducer = R.mergeWithKey(
  (k, l, r) => k == "foo" ? R.concat([l], [r]) : r
)

R.compose(
  R.map(R.reduce(reducer, [])),
  R.values,
  R.groupBy(R.prop("user"))
)(users)
there might be a more elegant way, but this does it
Mike Lambert
@lax4mike
Apr 15 2018 15:27
it's unclear what if user b should be { user: "b", foo: "bar" } or { user: "b", foo: ["bar"] }