javascript associative array
- Date
- 2005-11-08 (Tue)
- Category
- Tech
いつも忘れてしまうので、ここにメモ。javascript の連想配列の使い方。一応退屈な前説もするけれど、今回の僕のポイントとしては、連想配列を作るのに(他の言語にあるような)ショートハンドはなさそうだけれども、他に方法がある、ということ。
あと非プログラマには意味がわからない記述ではありますが、正確な記述と時間節約のためです。あしからず。
初期化
var a = new Array();
要素が空の Array オブジェクトを a という名前で初期化。
var a = new Array( int:num );
要素が num 個あるの Array オブジェクトを a という名前で初期化。ただしこれは a の length プロパティに num が代入されるだけで、各要素が空要素などで初期化されるわけではない。
var a = new Array( Object:e1, Object:e2... );
Constructor の引数に複数の値を指定すると、Array オブジェクトを a という名前で初期化し、引数の数だけ(実際には Arguments オブジェクトの length プロパティを参照)各要素を代入する。0から始まる数値が添字として自動的に割り振られるけれども、実際には添字は string 型である。ここでは参照のため Object 型を引数としたが、実際には primitive 型を複数個列挙することも可能。
連想配列(hash table, associative array)を作るには、以上の初期化を終えた上で、
a[String:key] = Object:value;
とすると key を添字とし、value を値となる。
例)
var a = new Array();
a[0] = "zero";
a["one"] = 1;
ただし、連想配列と似た動作をする Object を作成するのも可能である。たとえば、
var b = {
"zero":"0",
"one":1
};
こうすると、連想配列のようにもメンバ変数のようにも にアクセスできる。
例)
alert(b["zero"]); // "0" と表示。
alert(b.one); // 1 と表示。
思い出して JKL.ParseXMLをみると、[{...}]という記述があるので、javascript ではアクセサで、分岐しているんですね(当たり前ですが)。正確に Array として扱うときには [] をアクセサとして使えと。
Comment:12
- Coach Factory Outlet:2011-08-05 (Fri) 21:11
-
if your dream is to look like a million bucks for a mere several dollars, then our Coach Outlet store is the Jiminy Cricket of your existence.
- Louis Vuitton Bags:2011-08-05 (Fri) 22:19
-
There's the recent Louis Vuitton Purses intended for 2011 of Louis Vuitton shopping, Most of these purses possess the top quality & lowest cost.
- Louis Vuitton Outlet :2011-08-05 (Fri) 23:08
-
This Louis Vuitton Outlet Store is newly opened. There you can find whatever you like.
- Coach Factory Online:2011-08-06 (Sat) 00:33
-
Welcome to Coach Factory Store to enjoy your fantastic shopping experience.
- Swarovski Crystal:2011-08-06 (Sat) 02:26
-
The author provides complete step-by-step instruction for 20 projects and presents 45 more in a photo gallery of design variations, teaching readers how to vary colors and shapes to create custom effects about swarovski outlet.
- Coach Outlet :2011-08-06 (Sat) 03:33
-
At the Coach Outlet Online Store, quality products and satisfying service are provided;High safety and prompt delivery is guaranteed.
- coach outlet online:2011-08-19 (Fri) 05:32
-
Now can be the perfect time to choose a Coach Outlet Online. With fall getting best suited right here in numerous of weeks, something are on the way to acquire on clearance. But this appears to turn out to be the way in which by which you can appear throughout a little something at you Coach Handbags Outlet on sale. mentor utilizes the most significant best brilliant quality of factors to manufacture their products. We are the best Coach Purses Outlet agents.Welcome to buy in our online shop.More discount for you.
- air max shoes:2011-09-30 (Fri) 00:11
-
Hi, friends, sports you like, then to set the professional bar equipment. Really good.
- Moncler jackets:2011-09-30 (Fri) 01:59
-
Hi,my friends,welcome to choose and buy ,it is very great, I like it so much
- wholesale nfl jerseys:2011-10-07 (Fri) 02:16
-
Thanks for your sharing! I think you are very reasonable, What people want to see is the thing, some days ago, I have seen an article is about your writing. I think also is very good: is about. I think is also a need to share with everyone. Hope to be helpful to you. zhongchengmaoyi110post
- Louis Vuitton Outlet:2011-10-22 (Sat) 02:26
-
http://www.shop-louisvuitton.org
- Outlook 2010:2011-10-31 (Mon) 23:07
-
This article is GREAT it shows how misleading NFL ranks and stats can be EXCELLENT JOB and what a great tool!
Trackback:1
- TrackBack URL for this entry
- http://blogs.grf-design.com/mt/mt-tb.cgi/123
- Listed below are links to weblogs that reference
- javascript associative array from The Croton
- JavaScriptの変数とは? from ネットビジネス用CGI Perl HTML Javascriptの情報サイト : 2006-03-08 (Wed) 23:04
プログラムを初めて作成する方には「変数」という考え方は、あまり馴染みがないかもしれませんが、プログラムの世界では、当たり前の表現として使用してい...