javascript関連
概要
- 同じことをするのにたくさんのやり方がある
具体例
数値を特定小数点でroundする
> parseFloat("1.23456").toFixed(2);
'1.23'
arrayに要素が含まれているかどうか
> const fruits = ["Banana", "Orange", "Apple", "Mango"];
undefined
> fruits.includes("Mango");
true
> fruits.includes("XXX");
false
arrayのpopとshift
shift
> const fruits = ["Banana", "Orange", "Apple", "Mango"];
undefined
> fruits.shift();
'Banana'
> fruits
[ 'Orange', 'Apple', 'Mango' ]
pop
> const fruits = ["Banana", "Orange", "Apple", "Mango"];
undefined
> fruits.pop()
'Mango'
> fruits
[ 'Banana', 'Orange', 'Apple' ]
固定サイズのarrayを作成する
一次元のarray
> let array = Array(3).fill()
undefined
> array
[ undefined, undefined, undefined ]
二次元のarray
> let array = Array(5).fill().map(() => Array(3));
undefined
> array
[
[ <3 empty items> ],
[ <3 empty items> ],
[ <3 empty items> ],
[ <3 empty items> ],
[ <3 empty items> ]
]
arrayの要素をスワップ
> let list = ["a", "b", "c"]
undefined
> list
[ 'a', 'b', 'c' ]
> [list[0], list[2]]
[ 'a', 'c' ]
> [list[0], list[2]] = [list[2], list[0]]
[ 'c', 'a' ]
> list
[ 'c', 'b', 'a' ]