LoginSignup
2
2

More than 5 years have passed since last update.

配列の途中に配列を挿入する

Last updated at Posted at 2016-02-24

配列の途中に別の配列値を挿入するメソッド。
以外にありそうでなかったので書いておく。
(実はある!とかもっといい書き方、面白い書き方あったらコメントください。)

"use strict";

// destArrayのindexの位置にsrcArrayの内容を挿入
function insertArray(destArray,index,srcArray)
{
       srcArray.forEach((d,i)=>{
         destArray.splice(index + i,0,d);
       });
}

let dest = [0,3,4,5];
let src = [1,2];

insertArray(dest,1,src);
console.log(dest);
// [ 0, 1, 2, 3, 4, 5 ]

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2