pras
pras

Reputation: 115

Combine 2 arrays with different length, repeating the shorter array to match the length of the longer array

I want to ask how to merge 2 arrays with different lenght and extending the short one to match the long one's length.

A = [1,2,3,4,5,6,7,8,9,10]
B = ["a","b","c","d"]

result is array C
C = [
{1,"a"},
{2,"b"},
{3,"c"},
{4,"d"},
{5,"a"},
{6,"b"},
{7,"c"},
{8,"d"},
{9,"a"},
{10,"b"}
]

Thanks, sorry for my bad English

Upvotes: 0

Views: 175

Answers (1)

Rajneesh
Rajneesh

Reputation: 5308

You can use map:

const 
  A = [1,2,3,4,5,6,7,8,9,10],
  B = ["a","b","c","d"];
  
const result = A.map((o,i)=>[o, B[i%B.length]]);

console.log(result);

Upvotes: 1

Related Questions