-2

I have a Set of strings

const firstSet = new Set([
  "a",
  "b",
  "c",
])

and I would like to create a copy of it but also add with "d" and "e". I can do it like this:

const secondSet = new Set(firstSet)
secondSet.add("d")
secondSet.add("e")

but is there a shorter way?

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103

1 Answers1

-1

You can destructure the set, just like a list:

const secondSet = new Set([
  ...firstSet,
  "d",
  "e",
])
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103