Pedro
Pedro

Reputation: 43

How to combine elements from one array with each element from another array

I'm trying to combine elements from one array with every element from another array, I tried looking for some solutions but I couldn't figure it out.

Take these two arrays for example:

num = [1,2,3]

let = ["a","b","c"]

I want to combine them in order to obtain:

combined = [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], 
            [3, "a"], [3, "b"], [3, "c"]]

Upvotes: 2

Views: 380

Answers (1)

Viktor
Viktor

Reputation: 2783

You can use #product:

num = [1,2,3]
let = ["a","b","c"]

num.product let
#=>[[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]]

Upvotes: 7

Related Questions