0
0

More than 1 year has passed since last update.

コードを短く書く小技

Posted at

指定の文字列かどうかを判定したい

const color = 'red';

// before
console.log(color === 'yellow' || color === 'red' || color === 'blue');
// after
console.log(['yellow', 'red', 'blue'].includes(color));

引数の値に紐づく値を取得したい

// before
const translate = colorName => {
  switch(colorName) {
    case 'red':
      return '';
    case 'blue':
      return '';
    case 'yellow':
      return '';
    default:
      return '翻訳不可';
  }
}

// after
const translate = colorName => ({
  'red': '',
  'blue': '',
  'yellow': '',
}[colorName] || '翻訳不可')

// 実行例
translate('red');
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