Set
objects do not have a map()
method, so it's not surprising that you're getting errors. You might be thinking of the map()
method on Array
objects instead. A Set
is not an Array
, although both are iterable. Luckily you can convert any iterable into an Array
in several ways:
You can pass it to the static Array.from()
method:
const jobArray = Array.from(jobs); // Job[]
Or you can spread it into an array literal:
const jobArray = [...jobs]; // Job[]
Note that the values()
method of a Set
produces the very same iterator that is used when you treat the Set
as an iterator, so while you could use it, it's unnecessary:
const jobArray = Array.from(jobs.values()); // same thing
const jobArray = [...jobs.values()]; // also same thing
Once you have transformed the Set
into an Array
you can use map()
and then process the result as needed:
const max = Math.max(...jobArray.map(j => j.priority)); // number
Playground link to code
Set
indeed does not have amap()
method, and arrays have nomax()
method. You have to do something like this instead:Math.max(...Array.from(jobs).map((job) => job.priority))
– Silverware.reduce((acc, curr)=> acc>curr?acc:curr, 0)
but that's just because I need the default value to be 0 – Knotts0
because the wholemax
thing is out of scope for the question as asked). – Silverware