How do I fill elements of an array on another array with respecting the size Example
Arra1 = ["1","2","3","4","5"]
Arra2 = ["R","B"]
result = ["R","B","R","B","R"]
How can I achieve that
CodePudding user response:
You could do Array.map()
with modulo %
:
let Arra1 = ["1","2","3","4","5"]
let Arra2 = ["R","B"]
console.log(Arra1.map((num, ind)=>Arra2[ind%Arra2.length]))
//["R","B","R","B","R"]
CodePudding user response:
You can write this :
r = []
let j = 0
for (let i=0; i<Arra1.length; i )
{
if (j>= Arra2.length)
j = 0
r.push(Arra2[j])
j
}
Hope it helps.