filter関数
特定の条件に一致するデータのみを取得したい場合などに利用する。
const array = [1, 2, 3, 4, 5];
const result = array.filter((item) => {
return item > 2;
});
console.log(result); // [3, 4, 5]
以下のようにシンプルな記載方法もある。
const array = [1, 2, 3, 4, 5];
const result = array.filter(item => item > 2);
console.log(result); // [3, 4, 5]
map関数
配列内の全データを操作する場合に利用する。
const array = [1, 2, 3, 4, 5];
const result = array.map((item) => {
return item + 1;
});
console.log(result); // [2, 3, 4, 5, 6]
以下のようにシンプルな記載方法もある。
const array = [1, 2, 3, 4, 5];
const result = array.map(item => item + 1);
console.log(result); // [2, 3, 4, 5, 6]
forEach関数
配列内の全データを取得して何らかの処理する場合に利用する。
const array = [1, 2, 3, 4, 5];
array.forEach((item) => {
console.log(item);
});
以下のようにシンプルな記載方法もある。
const array = [1, 2, 3, 4, 5];
array.forEach(item => console.log(item));