0

I have myMap: { [key: string]: string[] } = {} and I want to remove a value for some certain key from myMap.

That is delete value1 from myMap[Key1].

I have done my google search but I failed to find it.

Jill Clover
  • 2,168
  • 7
  • 31
  • 51

2 Answers2

2

You can either remove both key and value:

myMap.delete(key);

Or reassign the value to undefined:

myMap.set(key, undefined);

Do note the first solution comes up with a Google search of javascript map delete value

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can do it in two ways:

  1. By using delete (mutation)
delete myMap[Key1]
  1. By using object destructuring (no mutation), however note that a possibly unwanted variable _ will be created.
const {[Key1]: _, ...updatedMyMap} = myMap
Wong Jia Hau
  • 2,639
  • 2
  • 18
  • 30
  • `delete` is used to delete the property of an object not an object value from the map. Or am i missing something here? – Lenzman Feb 03 '23 at 14:17