LoginSignup
0
0

More than 1 year has passed since last update.

勉強記録 3日目

Posted at

勉強三日目

勉強時間 35分

ドットインストール Javascript オブジェクト編
9回目 map

map()とは配列と関数を使って新しい配列を作るときに使う。
const 新配列名 = 元配列名.map((変数名を自由に決める) => { return 関数});

{
  const prices = [180, 190, 200];

  //省略前
 const updatedPrices = prices.map((price) => {
  return price + 20;});

  //省略後
  const updatedPrices = prices.map(price => price + 20);
  console.log(updatedPrices);
}

ポイント! いろいろな省略。
・アロー関数の場合、引数が一つなら変数の周りの()省略可
・式の場合は{}とreturn省略可
注意。省略しすぎて自分がわからなくなるとよくないため、省略はほどほどに。

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

10回目 filter
ある配列の一部を選んで新しい配列を作るときに使う。
配列の要素を変数として、filterの関数がtrueのときその要素は残り、falseのときその要素は排除される。

{
'use strict';

{
  const numbers = [1, 4, 7, 8, 10];

  // 省略前
  const evenNumbers = numbers.filter(number => {
     if (number % 2 === 0) {
       return true;
     } else {
       return false;
     }
   });


  //省略後
  const evenNumbers = numbers.filter(number => number % 2 === 0);

  console.log(evenNumbers);
}
}

省略は結局『number % 2 === 0』がtrueかfalseかという話なのでifのところを全部なくせる。
(number => number % 2 === 0)これですむ。

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