Map.delete() 方法在 JavaScript


Map 物件的 delete() 方法接收一個代表 map 元素鍵的字串,並從當前的 Map 物件中將其刪除。如果指定的鍵存在於 map 物件中,此方法返回 true;否則返回 false。

語法

其語法如下

mapVar.delete()

示例

 線上演示

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Java');
      mapVar.set('2', 'JavaFX');
      mapVar.set('3', 'HBase');
      mapVar.set('4', 'Neo4j');
      var map = mapVar.delete('4');
      document.write("Size of the map object: "+mapVar.size);
   </script>
</body>
</html>

輸出

Size of the map object: 3

示例

 線上演示

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Java');
      mapVar.set('2', 'JavaFX');
      mapVar.set('3', 'HBase');
      mapVar.set('4', 'Neo4j');
      var map = mapVar.delete('4');
      document.write(map);
      document.write("<br>");
      document.write("Size of the map object: "+mapVar.size);
      document.write("<br>");
      var map = mapVar.delete('5');
      document.write(map);
      document.write("<br>");
      document.write("Size of the map object: "+mapVar.size);
   </script>
</body>
</html>

輸出

true
Size of the map object: 3
false
Size of the map object: 3

更新於: 2020-06-25

5 千 + 瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.