How to combine two array using ramda library?

Related searches

I have two array as below

const l1 = [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ];
const l2 = [ {"d": 2}, {"e": 2}, {"f": 2} ];

How can I use ramda library to combine these two and get result like

[ {"a": 1, "a1": 2, "d": 2}, {"a": 1, "a1": 2, "e": 2}, {"a": 1, "a1": 2, "f": 2}, 
{"b": 3, "b1": 4, "d": 2}, {"b": 3, "b1": 4, "e": 2}, {"b": 3, "b1": 4, "f": 2}, 
{"c": 5, "c1": 6, "d": 2}, {"c": 5, "c1": 6, "e": 2}, {"c": 5, "c1": 6, "f": 2} ]

I used

R.map(R.xprod(__, l2, l1)

But not working since i got object inside array.

Thanks in advance.

To get a Cartesian product of two arrays of objects you can create a function by lifting merge:

const fn = R.lift(R.merge)

const l1 = [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ];
const l2 = [ {"d": 2}, {"e": 2}, {"f": 2} ];

const result = fn(l1, l2)

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>

Ramda Documentation, Creates a new list iteration function from an existing one by adding two new to the value at the given index of an array, returning a new copy of the array with the as const , constant , or K (for K combinator) in other languages and libraries. when only two functions are passed R.ap(R.concat, R.toUpper)('Ramda')� I looked at the docs and where, intersection, and merge seemed to be the ones to use. But intersection and merge expect the same key. where seems to be the ticket, but I'm still not sure how to tell it that I want two different keys to have the same value. So I figure I can do it mirroring the POJS way, using one or more of forEach, find, and

If not very particular about ramda, you can do as below with one line using flatMap and map.

const l1 = [
  { a: 1, a1: 2 },
  { b: 3, b1: 4 },
  { c: 5, c1: 6 }
];
const l2 = [{ d: 2 }, { e: 2 }, { f: 2 }];

const merged = l2.flatMap(item2 => l1.map(item1 => ({ ...item1, ...item2 })));
console.log(JSON.stringify(merged));

Function, Acts as a transducer if a transformer is given in list position. append List 1 // All arguments are passed through to the wrapped function takesOneArg(1, 2); //= > [1, 2] and will work with types that implement that spec. chain is also known as flatMap in some libraries forEach , Ramda's forEach returns the original array. Using Ramda should feel much like just using JavaScript. It is practical, functional JavaScript. We're not introducing lambda expressions in strings, we're not borrowing consed lists, we're not porting over all of the Clojure functions. Our basic data structures are plain JavaScript objects, and our usual collections are JavaScript arrays.

With plain Javascript, you could take cartesian product with a double nested reduce by mapping new objects.

This approach works for an arbirary count of arrays (sort of ...).

const
    l1 = [{ a: 1, a1: 2 }, { b: 3, b1: 4 }, { c: 5, c1: 6 }],
    l2 = [{ d: 2 }, { e: 2 }, { f: 2 }],
    result = [l1, l2].reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => ({ ...v, ...w }))), []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to combine two array using ramda library?, Creates a new list iteration function from an existing one by adding two new parameters Applies a function to the value at the given index of an array, returning a new copy of the array with the element at the This function is known as const , constant , or K (for K combinator) in other languages and libraries. append List. How to sum n-arrays by index using Ramda. 3. Merge two arrays with deeply nested key with Ramda. 0. Merge two arrays connected by an intermediate array with Ramda. 0.

concatAll � Issue #1897 � ramda/ramda � GitHub, I have two array as belowconst l1 = [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ];const l2 = [ {"d": 2}, {"e": 2}, {"f": 2} ];How can I use ramda� I came into contact with Ramda.js in the process of learning functional programming. I find that it is a very important library which provides a lot of useful methods, and I believe this is a tool that every JavaScript programmer should master.

I've read all previous attempts to have a variadic concat in the lib (#144, #149, # 151, . this issue as the function requested in the OP is part of the library. As a workaround, one can use flatten to concatenate multiple arrays. Ramda tutorial shows how to work with the Ramda library, which provides tools for advanced functional programming in JavaScript.

How to merge data in R using R merge, dplyr, or data.table See how to join two data sets by one or more common columns using base R’s merge function, dplyr join functions, and the speedy data

Comments
  • I love the elegance of lift! This is much cleaner than my first thought: pipe (xprod, map (apply (merge))).
  • I still struggle with R.lift, but this seems like a classic case. That was actually my 2nd thought - pipe(xprod, map(mergeAll)).
  • lift, ap, and chain are so powerful, but I don't always think to use them when I should. (And yes, obviously mergeAll would have been better than apply(merge); neither is close to as nice as the lift solution, thought.)
  • Why "sort of"? I haven't tested, but it looks as though it would work.