2
3

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)

Posted at

配列の要素の追加、削除のまとめです。JavaScript編です

'use strict';
    const scores = [80,90,40,70];
    // 要素の前に配列を追加
    scores.unshift(46,48);
    // 配列の要素の1番後ろを削除する
    scores.pop();

   for(let i=0;i<scores.length;i++){
    document.write(`<p> Score${i} : ${scores[i]} </p>`);
   }

unshiftメソッドで要素の1番前に要素を追加します。
popメソッドを使って要素の1番最後を削除します。

 'use strict';
    const scores = [80,90,40,70];
    // 要素の後ろに配列を追加
    scores.push(60,50);
    // 配列の要素の1番最初を削除する
    scores.shift();

   for(let i=0;i<scores.length;i++){
    document.write(`<p> Score${i} : ${scores[i]} </p>`);
   }

pushメソッドで要素の1番後に要素を追加します。
shiftメソッドを使って要素の1番最初を削除します。

2
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?