I am creating the map as follows:
var employeeMap = new Object();
addElement(employeeMap , 1, "ABC");
addElement(employeeMap , 2, "ABCD");
alert(getElement(employeeMap,1)); // ABC
function addElement(map,key,value){
map[key] = value;
return map;
}
function getElement(map,key){
return map[key];
}
How to delete a key-value pair from the map ? Is setting the map[key] = null the only option ?
Thanks,
Shikha
To actually remove the key/value pair, use
delete: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/deleteSo you could create a function to remove elements:
And call it like:
UPDATE:
But as I’m reading,
deletereturnstrueif it’s successful, andfalseonly if it cannot be deleted (for several reasons). So i guess the real version would be what the other answer has: