How do you shallow-clone a Map or Set object in JavaScript?
I want to get a new Map or Set that has the same keys and values.
Use the constructor to shallow-clone Maps and Sets:
let clonedMap = new Map(originalMap);
let clonedSet = new Set(originalSet);
Note: Keep in mind that while this is very fast, any mutable nested objects will be shared between the original and the clone. If you want to clone recursively, as of 2022 I recommend using structuredClone(originalMapOrSet)
as per this answer about deep-cloning.