2
0

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 5 years have passed since last update.

【JavaScript】配列の操作いろいろ②

Last updated at Posted at 2020-05-12

配列の要素をループ処理する##

  • forEach( )を使う
//配列.forEach((item) => {
  // itemに関する処理
  // itemは、配列の中身のうち、任意の1つを指し示す
//}
//プログラム中の「item」とは、「配列の中身のうちの1つ」という意味 名前は何でも良い
const fruits = [ "apple", "banana", "orange", "melon" ];
    //アロー関数とテンプレートリテラルを使って取り出してみる
  fruits.forEach((item) => {
  console.log(`Fruits: ${item}` ); 
 });
//実行結果
//Fruits: apple
//Fruits: banana
//Fruits: orange
//Fruits: melon

配列の要素に何らかの処理をしてそれを別の配列として定義する##

  • map( )を使う
const prices = [180,190,200];
  //新しい配列にpricesの要素に20足したものを代入
const upDatedPrices = prices.map(price => prices + 20);
console.log(upDatedPrices);
//実行結果
//200,210,220

配列の要素をチェックして条件に合うものだけを取り出す##

  • filter( )を使う
const numbers = [1,4,7,8,10];
  //numberを2で割った余りが0の数だけreturnする
const newNumbers = numbers.filter(number => number % 2 === 0);

console.log(newNumbers);
//実行結果
//4,8,10

配列をオブジェクトで作る##

  • キーを使う
//プログラム中のchocolateやcreamがキーとなる
//180や210はそのキーの値となる。
const bread = {
  chocolate: 180, 
  cream: 210
};
console.log(bread);
//実行結果
//chocolate: 180, 
//cream: 210
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?