0
1

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の連想配列のメンバ数取得方法

0
Last updated at Posted at 2018-04-24

こちらの記事を参考にさせていただきました
https://javascript.programmer-reference.com/js-associative-array-length/

Object.keysを使う方法

var len = Object.keys(rensoArr).length;

for inを使う方法

var len = 0;
for(var p in rensoArr){
  if(rensoArr.hasOwnProperty(p)){
    len++;
  }
}

コードの長さ的には前者だけれど、一度keyを配列にしているということはメモリ的に結構無駄が発生しているのでは?
と思うので後者を関数化して使う方が良い気もします。何をしたいのかもわかりやすくなるし。

恥ずかしくても書いてみよう

3ヶ月後に恥ずかしくないように

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?