0
0

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 5 years have passed since last update.

JavaScriptで連想配列を作る時の注意点

Last updated at Posted at 2019-08-23

#前書き#
こんにちは!
最近をJavaScriptを使う機会が多く連想配列で詰んだのでその時の解決策を書き記します(^^)
初心者なので暖かく見守ってください!w

#例#

まず、連想配列とはキーと値がペアになったデータのことです。

phpでは、例えば次のように書くことが可能です ( 他にも方法はあります )

$test = [ ];
$test['a'] = a;
$test['b'] = b;
$test['c'] = c;

なので、JavaScriptでも次のように書いてしまいがちですがこれだと作れません(>_<)

var test = [ ];
test['a'] = 'a';
test['b'] ='b';
test['c'] = 'c';

何故かというと、JavaScriptの場合はオブジェクトの生成が{ }、配列の生成が[ ]となっています
なので、正しくは

var test = { };
test['a'] = 'a';
test['b'] = 'b';
test['c'] = 'c';

これだといけます(^^)

ちなみに、こんな書き方もあるみたいです~

var test= new Object();
test.a = 'a';
test.b = 'b';
test.c = 'c';
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?