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 1 year has passed since last update.

【備忘録】Javascriptでオブジェクトをfor in文で回す際の注意点

Posted at

Qiita初投稿です。

for in文の中で オブジェクト名.プロパティ名としても上手くいかなかった

let feature = {age :25, weight:65, name:'Tanaka'};

for(let p in feature) {
    console.log(p + '=' + feature.p);
}
/*出力結果
age=undefined
weight=undefined
name=undefined
*/

原因

feature.pとかくと、featureのpプロパティという意味になるみたい。

ここでは、呼びたいのはfeatureオブジェクトのプロパティage,weight,name

正しい書き方

let feature = {age :25, weight:65, name:'Tanaka'};

for(let p in feature) {
    console.log(p + '=' + feature[p]);
}
/*出力結果
age=25
weight=65
name=Tanaka
*/

思ったとおりの結果になった。

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?