0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

js 配列に別の配列の新たな数を追加していく

Last updated at Posted at 2017-01-05

##お題
・配列に別の配列の新たな数を追加していく
・新たな数は最後尾に順に足していく

function uniteUnique(arr) {
//write your code.
   return newArr;                    
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) // [1, 3, 2, 5, 4]

##出力結果 例

([1, 3, 2], [1, [5]], [2, [4]]) // [1, 3, 2, [5], [4]]
([1, 2, 3], [5, 2, 1]) // [1, 2, 3, 5]
([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) // [1, 2, 3, 5, 4, 6, 7, 8]

##使ったもの
arguments
reduce()
concat()
indexOf()
slice()

##考え方
・Array.prototype.slice.call(arguments)で複数の引数を変数(args)にいれてまとめて扱う。
・reduce()でargsをはじめから順に比較していく。
・filterにてはじめの引数(arrA)にない値を次の引数(arrB)から選び、concatで付け足す。
・新たな値は新しく用意した変数に入れて、それを返しておわり

##コード

function uniteUnique(arr) {
 var newArr;
  var args = Array.prototype.slice.call(arguments);
  newArr = args.reduce(function(arrA,arrB){
    return arrA.concat(arrB.filter(function(i){
      return arrA.indexOf(i) === -1;
    }));
  });

   return newArr;                    
}

###他にもコードが浮かんだ方、コメントお待ちしております。

0
0
3

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?