ฟังก์ชัน delete() ของออบเจ็กต์ Map ยอมรับสตริงที่แสดงถึงคีย์ขององค์ประกอบของแผนที่และลบออกจากออบเจ็กต์ Map ปัจจุบัน ฟังก์ชันนี้จะคืนค่า จริง หากคีย์ที่ระบุมีอยู่ในอ็อบเจ็กต์แผนที่ มิฉะนั้น ฟังก์ชันนี้จะคืนค่าเป็นเท็จ
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
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