Map Set


Map


Mapオブジェクトは、データの収集と利用のためのオブジェクトです.[キー](Key)と[値](Value)のペアをMapオブジェクトに格納して使用します.
Mapでは、キーは一意の値であり、キーと値のデータ型には制限はありません.

Map vs Object

  • オブジェクトはキーとしてのみ文字列を使用できますが、Mapのキータイプには制限はありません.
  • Mapオブジェクトはハッシュ・テーブルを使用するため、データの取得速度が速い.
  • Mapオブジェクトはsize propertyを使用してデータ数を取得できますが、オブジェクトオブジェクトは手動で計算する必要があります.
  • 方法


  • 新Map()–マッピングを作成します.

  • map.set(key,value)-keyを使用してvalueを保存します.
    map[key]はmapを書く正しい方法ではありません。 map[key]は、map[key]=2に設定したように使用できますが、この方法ではmapを通常のオブジェクトと見なします。そのため様々な制約が生じる。 mapを使用する場合は、map専用メソッドセット、getなどを使用する必要があります。


  • map.get(key)-keyに対応する値を返します.キーが存在しない場合はundefinedを返します.

  • map.has(key)-keyが存在する場合はtrueを返し、存在しない場合はfalseを返します.

  • map.delete(key)-keyに対応する値を削除します.

  • map.clear()–マッピング内のすべての要素を削除します.

  • map.size–要素の数を返します.

  • map.keys()–各要素にキーがある反復可能なオブジェクトを返します.

  • map.values()–各要素の値を収集する小さなかわいいオブジェクトを返します.

  • map.entries()–要素の[キー、値]のペアを持つ小さなオブジェクトのペアを返します.このスワップ可能なオブジェクトは...複文の基礎とする.
  • let recipeMap = new Map([
        ['cucumber', 500],
        ['tomatoes', 350],
        ['onion',    50]
      ]);
      console.log(recipeMap);
      console.log(recipeMap.keys());
      console.log(recipeMap.values());
      console.log(recipeMap.entries());
      
      // 키(vegetable)를 대상으로 순회합니다.
      for (let vegetable of recipeMap.keys()) {
        console.log(vegetable); // cucumber, tomatoes, onion
      }
      
    //결과
    
    Map(3) {"cucumber" => 500, "tomatoes" => 350, "onion" => 50}
    
    MapIterator {"cucumber", "tomatoes", "onion"}
    MapIterator {500, 350, 50}
    MapIterator {"cucumber" => 500, "tomatoes" => 350, "onion" => 50}
    
    cucumber
    tomatoes
    onion

    Set


    Setオブジェクトは、重複しない唯一のオブジェクトである.
    セット値のデータ型には制限はありません.
    オブジェクトタイプ、元のタイプのどちらでも構いません.
    let set1 = new Set();
    console.log(set1) 
    //-> Set { }
    let set2 = new Set(["oranges", "apples", "bananas"]);
    // -> Set(3) {"oranges", "apples", "bananas"}
    let set3 = new Set();
    
    let john = { name: "John" };
    let pete = { name: "Pete" };
    let mary = { name: "Mary" };
    
    set3.add(john);
    set3.add(pete);
    set3.add(mary);
    
    //Set {{name: "John"},{name: "Pete"},{ name: "Mary" }}

    方法

    let set = new Set();

  • set.add(value)-値を追加し、グループ自体を返します.

  • set.delete(value)-値を削除します.呼び出しポイントリソースに値があり、削除に成功した場合はtrueまたはfalseを返します.

  • set.has(value)-グループに値がある場合はtrueまたはfalseを返します.

  • set.clear()–3つクリアします.

  • set.size–3つの値を数えます.

  • set.values()

  • set.keys()
  • Setでは、値()とキー()が同じです.

    forEach, for..of

  • for..of
    呼び出された値はvalueです.
  • for(let value of set1){
    	console.log(value)
    }
  • forEach
  • set1.forEach((value1, value2, set) => {
      console.log(value1);
    });