LoginSignup
0
0

More than 1 year has passed since last update.

【JavaScript関数ドリル】中級編のconcat関数の実装アウトプット

Posted at

concat関数の課題内容

_.concat関数を自分で実装する課題。
https://lodash.com/docs/4.17.15#concat

「課題内容」/「解説動画」/「解答例」を確認したい場合は、以下リンク先のページを参照。
https://js-drills.com/blog/concat/

課題に取り組む前の状態

  • 解答例を見なくてもできそうと思った。

課題に取り組んだ後の状態

  • 解答例ではforループ内でも変数をしようしているのを除いては、同じように実装できたので、よかった。

concat関数の実装コード(答えを見る前)

function concat(array, ...values) {
    const concatenatedArray = [...array];

    for (let i = 0; i < values.length; i++) {
        if (Array.isArray(values[i])) {

            concatenatedArray.push(...values[i]);
        } else {
            concatenatedArray.push(values[i]);
        }
    }

    return concatenatedArray;
}

var array = [1];
var other = concat(array, 2, [3], [[4]]);
 
console.log(other);
// => [1, 2, 3, [4]]
 
console.log(array);
// => [1]

concat関数の実装コード(答えを見た後)

// 同じ
0
0
0

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