LoginSignup
0
2

More than 1 year has passed since last update.

for...in文

Last updated at Posted at 2022-12-12
  <script>
    let jsbook = {title: 'Javascript入門', price: '2500', stock: '3'};
    for(let p in jsbook) {
        console.log(p + '=' + jsbook[p]);
    }
  </script>

コンソール

title=Javascript入門
price=2500
stock=3

気づき

let pにプロパティのキーが与えられていると思う。

出典

for...in文

オブジェクトのプロパティを全て読み取ることだけを目的とした、専用の繰り返し文

HTMLに出力する

<section>
    <table border="1">
      <tr>
        <td id="title"></td>
        <td id="price"></td>
        <td id="stock"></td>
      </tr>
    </table>
  </section>
<script>
      'use strict';
    let jsbook = {title: 'Javascript入門', price: '2500', stock: '3'};
    for(let p in jsbook) {
        if(p === 'price'){
            document.getElementById(p).textContent = `${jsbook[p]}円`;
        }else
        document.getElementById(p).textContent = jsbook[p];
    }
  </script>

これは自分で少しいじりました。
勉強のためにも...

気づき

オブジェクトのキーを代入したいときはp
プロパティのキーの値を代入したい時はオブジェクト名[プロパティのキー]

感想

ついに自分でコードを簡単に弄れるようになってきた。
この調子で頑張ろう。

出典

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