當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaScript Map.delete()用法及代碼示例

Map 對象的 delete() 函數接受一個表示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

相關用法


注:本文由純淨天空篩選整理自Karthikeya Boyini大神的英文原創作品 Map.delete() function in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。