Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

"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 ]

SFPGMR
プログラミングを趣味の一つとしてます。得意分野はなく、そのとき興味を引いたものをいじって楽しんでおります。つまりすべての分野においてド素人ということです。 保有資格:普通運転免許・ネットワークスペシャリスト・情報処理第一種・第二種
http://sfpgmr.net/blog/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした