Shallow Clone a Map or Set in JavaScript
To shallow clone a map or a set, we can just pass in the original map or set into the Map
or Set
constructor respectively.
For instance, we can write:
const originalMap = new Map([
['foo', 1],
['bar', 2]
])
const clonedMap = new Map(originalMap)const originalSet = new Set([1, 2, 3])
const clonedSet = new Set(originalSet)