0 votes
in JavaScript by
How to Clone a Map or Set in JavaScript?

1 Answer

0 votes
by

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)
...