DEV Community

Discussion on: Typical JavaScript interview exercises (explained)

Collapse
 
richseviora_12 profile image
Rich Seviora • Edited

To be very thorough:

Object.freeze(heroes);
const newHeroes = heroes.map(h => {
  Object.freeze(h);
  return { ...h, name: h.name.toUpperCase() }
})
Collapse
 
jeffreyducharme profile image
JDucharme

This approach is the only one that seems to work for me.

Collapse
 
dwmiller profile image
David Miller

Why would you need to freeze it? This stuff is synchronous, how would it change in the middle of your operation?

Collapse
 
richseviora profile image
Rich Seviora

It's to guard against future changes. But the requirement was to "preserve immutability" and so this makes the original data set immutable going forward.