Help us understand the problem. What is going on with this article?

JavaScript-dotinstall オブジェクト①

オブジェクト

配列

配列の生成

{
  const scores = [80,90,100]
  console.log(scores);
}

結果

Array(3)
0: 80
1: 90
2: 100

配列へのアクセスと要素の変更

{
  const scores = [80,90,100]
  console.log(scores);
  console.log(scores[0]);//変更前
  scores[0] = 1;
  console.log(scores[0]);//変更後
 console.log(scores.length);//要素の数
}
(3) [80, 90, 100]
80
1
3

** constは scores = 10;などと値の再代入はできないが、要素の変更はできる。

配列の反復処理

{
  const scores = [80,90,100];
  // console.log(`Score; ${scores[0]}`)
  // console.log(`Score; ${scores[1]}`)
  // console.log(`Score; ${scores[2]}`)
  //for文で書き換える
  for(let i = 0; i < scores.length; i++){
    console.log(`Score: ${scores[i]}`);
  }
}
Score: 80
Score: 90
Score: 100

配列要素の追加・削除

  • unshift 先頭に追加(a,b);
  • shift 末尾から削除();
  • push 末尾に追加(a,b);
  • pop 末尾から削除();
{
  const scores = [80,90,100];
  //先頭追加unshift / 削除shift
  scores.push(60,50); //80,90,100,60,50
  scores.shift(); //90,100,60,50
  //末尾追加push / 削除 pop
  for(let i = 0; i < scores.length; i++){
    console.log(`Score ${i}:${scores[i]}`);
  }
}
Score: 90
Score: 100
Score: 60
Score: 50

配列要素の変更

spriceを使う a.sprice(変化が始まる位置(index),削除数,追加したい要素);

{
  const scores = [80,90,100];
  scores.splice(1,0,40,50); //90(index = 1 )の次に40,50を挿入。削除数は0
  for(let i = 0; i < scores.length; i++){
    console.log(`Score ${i}:${scores[i]}`);
  }
}
Score 0:80
Score 1:40
Score 2:50
Score 3:90
Score 4:100

スプレッド演算子

{
  const otherScores = [10,20];
  // const scores = [80,90,100];
  //otherScoresをscoresに入れたい!
  //scores = [80,90,100,otherScores]としてしまうと、
  //80,90,100,Array[2]の形になってしまうので、スプレッド構文を使う。
  const scores = [80,90,100,...otherScores];
  console.log(scores);
  }
(5) [80, 90, 100, 10, 20]

引数に展開

function sum(a,b){
    console.log(a+b);
  }
  sum(...otherScores);
  //sum(10,20)と同義
30

分割代入

{
  const scores = [80,90,40,70];
  const [a,b,c,d] = scores;
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
}

scoresの中身80,90,40,70を一回の記述で代入できる。

80
90
40
70

分割代入しないパターン

  const scores = [80,90,40,70];
  const a = scores[0];
  const b = scores[1];
  const c = scores[2];
  const d = scores[3];
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした