0
0

More than 3 years have passed since last update.

【JavaScript】配列とオブジェクトの違い

Last updated at Posted at 2020-05-03

【JavaScript】配列とオブジェクトの違い

配列

複数の値(要素)を[]の中に入れ、参照したり、追加削除して利用する


    let values = [1,2,3,4,5];
    console.log(values[0]);//1

    let cities = ['Tokyo','Kyoto','Sapporo','Fukuoka','Udon'];
    console.log(cities[1]);//Kyoto

初めに[]で器を作り、push()で末尾に追加して利用したり、初期化時に[]で上書きする使われ方が多い印象。
一時的な入れ物。

以下はRPGツクールMVのスイッチの初期化処理。
[]することでスイッチの情報を初期化(ALL off状態)している


    Game_Switches.prototype.clear = function() {
        this._data = [];
    };

Game_Picture.prototype.tint内の色調(RGB)の初期化では0を入れている

    if (!this._tone) {
        this._tone = [0, 0, 0, 0];
    }

オブジェクト

複数の値をプロパティ付きで管理する。
プロパティ=名前ラベルと理解。

キャラクター座標をcharacterPositionという定数にx:100,y:200と設定した例。


    const characterPosition ={
        x:100,
        y:200
    }

参照方法は2通り。
・オブジェクト名.プロパティ名
・オブジェクト名["プロパティ名"]
characterPositionのx座標を参照するには、characterPosition.xとする。

    console.log(characterPosition.x);//100
    console.log(characterPosition["y"]);//200

高さz軸を追加する場合はzプロパティを追加して値を入れれば良い。


    const characterPosition ={
        x:100,
        y:200,
        z:300
    }
0
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
0
0