Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page >

配列とオブジェクト

JavaScriptは、データを収集するための配列を提供しています。配列はオブジェクトであるため、これを使うには次のように配列の新しいインスタンスを作成しておく必要があります。

var myArray = new Array();

あるいは、次のようにもっとシンプルな構文を使うことができます。

var myArray = [];

配列を作成したら、次のように括弧で囲んだ整数を使って要素を参照します。

myArray[0] = "first value";
myArray[1] = 5;

上記の例は、配列は変数と同様に、任意の型のデータを保持できることを示しています。

汎用のオブジェクトと配列を、インデックス番号の代わりに文字列を使う連想プロパティと組み合わせて使うことができます。

myObject["indexOne"] = "first value";
myObject["indexTwo"] = 5;

文字列をインデックスとして使う場合は、括弧の代わりにピリオドを使ってそのプロパティにアクセスし、データを代入することができます。

myObject.name = "Apple Inc.";
myObject.city = "Cupertino";

オブジェクトとそのコンテンツをインラインで宣言することもできます。

myObject = {
    name: "Apple Inc.",
    city: "Cupertino"
}

forループのバリエーションを、配列またはオブジェクトのプロパティ内での反復に利用できます。for-inループと呼ばれるループで、次のような形式をとります。

for ( var index in myArray )
{
    // このループ内で実行するコード
}


< Previous PageNext Page >


Last updated: 2007-06-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice