Shallow-clone an ES6 Map or Set

2020-02-17 06:43发布

问题:

How do you shallow-clone an ES6 Map or Set object?

I want to get a new Map or Set that has the same keys and values.

回答1:

Use the constructor to clone Maps and Sets:

var clonedMap = new Map(originalMap)

var clonedSet = new Set(originalSet)