-2

i would like to merge array a in array b on index 2.

let a = [1,2,7,8,9,10]; let b = [3,4,5,6];

How should i write my code?

Tia

Bruna Piloto
  • 19
  • 1
  • 8
  • You can find more posts [here](https://www.google.com/search?q=merge+2+arrays+by+index+js+site:stackoverflow.com&rlz=1C1GGRV_enIN767IN767&sxsrf=ALeKk03DEFmJe4QTbBpr_cALARXz3pkc2w:1583231736890&sa=X&ved=2ahUKEwj5xJjqjf7nAhW9_XMBHQNqD8IQrQIoBDACegQIAhAO&biw=2560&bih=1299) – Rajesh Mar 03 '20 at 10:36
  • 2
    It's not clear. What is the expected output? – adiga Mar 03 '20 at 10:40

1 Answers1

1

Use the splice method.

let a = [1,2,7,8,9,10]; 
let b = [3,4,5,6];

b.splice(2, 0, ...a);

console.log(b);
Siva K V
  • 10,561
  • 2
  • 16
  • 29