-2

I am new for underscorejs,

First I have two arrays =>

var array1 = [1,2];

var array2 = [1,2];

How can I get like =>

[[1,1],[1,2],[2,1],[2,2]]

by using underscorejs?

Loran
  • 772
  • 4
  • 16
  • 38

1 Answers1

0

What you are trying to achieve is called Cartesian Product. Underscore is not required, nor it has such util function.

a.map(_a => b.map(_b => [_a, _b])).flat()

Or

a.flatMap(_a => b.map(_b => [_a, _b]));

const a = [1, 2], b = [1, 2];
const out = a.map(_a => b.map(_b => [_a, _b])).flat();
console.log(JSON.stringify(out));
Newbie
  • 4,462
  • 11
  • 23