7
10

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・初学者】Reactでよく使われる頻出記法 / forEachについて

Last updated at Posted at 2023-11-14

はじめに

Reactを学習していましたが、JavaScriptの記法についてフワッとしている部分がよくあったので、簡単ですが今回はforEachについて少し調べました。

forEachの使い方

基本的な使い方は配列に対して要素を1つずつ取り出し処理を実行をします。

let fruit = ['Apple', 'Melon', 'Orange'];

fruit.forEach(element => console.log(element));

// Apple
// Melon
// Orange

また、forEachの引数はこのように取れます。

配列名.forEach( コールバック関数(要素の値, 要素のインデックス, 配列) )
let fruit = ['Apple', 'Melon', 'Orange'];

fruit.forEach(function(element, index, array){
  console.log('Index:' + index);
  console.log('Element:' + element);
  console.log('Array:' + array);
});

//  Index:0
//  Element:Apple
//  Array:Apple,Melon,Orange
//  Index:1
//  Element:Melon
//  Array:Apple,Melon,Orange
//  Index:2
//  Element:Orange
//  Array:Apple,Melon,Orange
7
10
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
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?