logo

辞書を作成し、キーと値のペアを動的に追加するにはどうすればよいですか?

この記事では、オブジェクトを使用してキーと値のペアを格納することにより、JavaScript で辞書を作成する方法を説明します。 JavaScript には組み込みの辞書タイプがありませんが、JavaScript オブジェクトを使用して効果的に辞書タイプを作成できます。まず、辞書として機能する新しい JavaScript オブジェクトを作成しましょう。

Javaプライオリティキュー

構文:

キーには文字列または整数を指定できます。 key1 または任意の数字を記述するだけでは、文字列として扱われます。



var dict = { key1 : value1 , key2 : value2 , .... };>
  • 空の辞書を作成する
    var dict = {};>
  • ディクショナリへのキーと値のペアの追加
    dict[new_key] = new_value;>
    または new_key がすでに辞書に存在する場合、この値は new_value に更新されます。
    dict.new_key = new_value;>
  • キーと値のペアへのアクセス
    var value = dict[key];>
    または
    var value = dict.key;>
  • 辞書全体を反復する
    for(var key in dict) { console.log(key + ' : ' + dict[key]); }>

例:

HTML
   Javascript の辞書title> head> <body style='text-align: center;'> <h1 style='color: green;'>techcodeview.com h1><p>var dict = { <br />「オタク」 : 1 、 <br />'for' : '2', <br />「オタク」 : 3.5 <br />}; <br />p> <button onClick='fun()'>新しいキーと値のペアを追加ボタン><p id='demo'>p><script>function fun() { var dict = { オタク: 1, for: '2', オタク: 3.5, };  dict.new_geeks = 'new_value';  dict['another_new_geeks'] = 'another_value';  var to_show = 'var dict = { ';  for (var key in dict) { to_show += ''' + key + '' : ' + dict[key] + ' ';  to_show += ' }; ';  document.getElementById('demo') .innerHTML = to_show;  } スクリプト>本体>html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/javascript-misc/88/how-create-dictionary.webp' alt="">  <br></article><div class="rekl_placeholder"><script type="text/javascript">atOptions = {'key' : 'f2b09f3e7178b263531e10998e9a32fa','format' : 'iframe','height' : 250,'width' : 300,'params' : {}};</script><script type="text/javascript" src="//spiritscaution.com/f2b09f3e7178b263531e10998e9a32fa/invoke.js"></script></div> </article>
                         
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
    
            </div><!--//primary-->
            <div class="secondary col-md-4 col-sm-12 col-xs-12">
                  <aside class="info aside section">
                    <div class="section-inner">
                        <h2 class="">カテゴリ</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li> <a href="/english-grammar/">英語の文法</a> </li><li> <a href="/dot-net/">ドットネット</a> </li><li> <a href="/data-link-layer/">データリンク層</a> </li><li> <a href="/numbers/">数字</a> </li><li> <a href="/javascript-tutorial/">Javascript チュートリアル</a> </li><li> <a href="/python-dictionary-programs/">Python 辞書プログラム</a> </li><li> <a href="/c-misc/">C-その他</a> </li><li> <a href="/c-storage-classes-type-qualifiers/">C ストレージ クラスと型修飾子</a> </li><li> <a href="/installation-mac/">Mac へのインストール</a> </li><li> <a href="/gmail/">Gメール</a> </li>
                                
                            </ul>
                        </div><!--//content-->  
                    </div><!--//section-inner-->                 
                </aside><!--//aside-->
                
                 <aside class="testimonials aside section">
                	 <div class="section-inner">
                        <div class="content">
                            <div class="item">
  								<div class="rekl_placeholder" id="sidebar_placeholder_1"> <script type="text/javascript">atOptions = {'key' : 'f53f1abf3c3a788f849c12a956865db1','format' : 'iframe','height' : 300,'width' : 160,'params' : {}};</script><script type="text/javascript" src="//spiritscaution.com/f53f1abf3c3a788f849c12a956865db1/invoke.js"></script> <br></div>                                                   
                            </div>
                        </div>
                    </div>
                   <div class="section-inner">
                        <h2 class="heading">JSON配列とは何ですか?</h2>
                        <div class="content">
                            <div class="item">
                                 <blockquote class="quote">                                  
                                    <span> <i class="fa fa-quote-left"></i> オタク向けのコンピューター サイエンス ポータル。これには、よく書かれ、よく考えられ、よく説明されたコンピューター サイエンスとプログラミングの記事、クイズ、練習/競技プログラミング/企業面接の質問が含まれています。</span>
                                </blockquote>                
                                                                                             
                            </div><!--//item-->
                            
                            <p> <a class="more-link" href="/what-is-json-array"> <i class="fa fa-external-link"></i> 続きを読みます</a> </p> 
                            
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
               
                            
                 <aside class="languages aside section">
                    <div class="section-inner">
                        <h2 class="heading">興味深い記事</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li class="item">
                                    <span class="title"> <strong> <a href="/how-many-ounces-100-milliliters">100ミリリットルは何オンスですか?</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/what-is-recycle-bin">ごみ箱とは何ですか?</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/top-10-most-technologically-advanced-countries-2024">2024 年最も技術的に先進的な国トップ 10</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/mysql-delete-cascade">MySQL オン削除カスケード</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/maths-class-10-cat/">数学-クラス-10</a> </strong> </span>
                                    
                                </li><!--//item-->
                               
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
              
                 <aside class="list music aside section">
                    <div class="section-inner">
                        <h2 class="heading">人気の投稿</h2>
                        <div class="content">
                            <ul class="list"> <li><a href="/java-arrays">Javaメソッドの配列</a>
</li><li><a href="/java-switch-statement">Javaのスイッチを入れる</a>
</li><li><a href="/javascript-window-open-method">javascript window.open</a>
</li><li><a href="/numpy-log-python">ぎこちないログ</a>
</li><li><a href="/binary-search-algorithm">二分探索のアルゴリズム</a>
</li><li><a href="/c-programs">Cプログラミングのサンプルプログラム</a>
</li><li><a href="/katrina-kaif">カトリーナ・カイフ</a>
</li> 
                                
                                
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
              
            </div><!--//secondary-->    
        </div><!--//row-->
    </div><!--//masonry-->
    
	    <footer class="footer">
        <div class="container text-center">
                <span>
Copyright ©2025 全著作権所有 |  <a href="//es.techcodeview.com/">techcodeview.com</a> | <a href="/disclaimer" rel="nofollow noopener noreferrer" target="_blank">免責事項</a>  |  <a href="/about-us" rel="nofollow noopener noreferrer" target="_blank">私たちについて</a>  |  <a href="/privacy-policy" rel="nofollow noopener noreferrer" target="_blank">プライバシーポリシー</a>  </span>
        </div>
    </footer>
 
         
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/bootstrap/js/bootstrap.min.js"></script>    
    

    <script type="text/javascript" src="https://techcodeview.com/template/assets/js/main.js"></script>     
	
	<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
	<script>
!function(){"use strict";let t=document.createElement("button");t.id="toTopBtn",t.innerHTML="↑";let e=`
        #toTopBtn {
            position: fixed;
            bottom: 25px;
            right: 25px;
            z-index: 9999;
            opacity: 0;
            visibility: hidden;
            background-color: #213141;
            color: white;
            border: none;
            border-radius: 8px;
            width: 50px;
            height: 50px;
            font-size: 24px;
            cursor: pointer;
            transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
        }
        #toTopBtn:hover {
            background-color: #555;
        }
    `,i=document.createElement("style");i.type="text/css",i.innerText=e,document.head.appendChild(i),document.body.appendChild(t),window.addEventListener("scroll",()=>{let e=window.scrollY||document.documentElement.scrollTop;e>300?(t.style.opacity="1",t.style.visibility="visible"):(t.style.opacity="0",t.style.visibility="hidden")}),t.addEventListener("click",()=>{window.scrollTo({top:0,behavior:"smooth"})})}();
</script>
</body>
</html>