1
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 1 year has passed since last update.

【JavaScript関数ドリル】Lodash関数の実装【勉強用】_.initial関数

Posted at

【JavaScript関数ドリル】初級編の_.initial関数の実装のアウトプット

_.initial関数の挙動
_.initial([1, 2, 3]);
// => [1, 2]

渡された配列の末尾を取り除いたものを返す

_.initial関数の課題内容

_.initial関数に取り組む前の状態

  • 元の配列をコピーし、末尾をpopするだけで良さそう

_.initial関数に取り組んだ後の状態

  • スプレッド構文(...array)について、複数の配列をまとめて受け取るという使い方しか知らなかったので、コピーにも使えることがわかった
  • 概ね解答と同じ書き方で、短時間で作成できた
  • 2日ぶりにコードを書いただけでアロー関数の書き方も少し忘れていたので、やはり関数ドリルだけでも毎日取り組もうと思った

_.initial関数の実装コード

const initial = (array) => {
    const popedArray = array.slice();
    popedArray.pop();
    return popedArray;
};

console.log(initial([1, 2, 3, 4, 5, 6]));
// => [1, 2, 3, 4, 5]

_.initial関数の解答コード

function initial(array) {
  const copiedArray = [...array];
  copiedArray.pop();

  return copiedArray;
}

const numbers = [1, 2, 3];
console.log( initial(numbers) );
// => [1, 2]

console.log('number : ', numbers);
1
0
1

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
1
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?