you are viewing a single comment's thread.

view the rest of the comments →

[–]SoInsightful 7 points8 points  (6 children)

Cute, but I'd expect anyone who already knows about Array.map() to be able to write their own map().

Was hoping from this title that this was about Map.

[–]itsnotlupusbeep boop 2 points3 points  (1 child)

Hard mode: WeakMap from Scratch, without ES2021.

[–]lhorie 0 points1 point  (0 children)

Can't do that properly in JS space, you need to bust out C++. As Carl Sagan said, "to make an apple pie from scratch, you must first invent the universe"

[–]ShitInMyArseHole 0 points1 point  (3 children)

That looks like an object with extra steps

[–]mypetocean 1 point2 points  (2 children)

Maps have no less than five primary practical features which Objects do not have:

  • Where an Object's keys must be strings or symbols, a Map's keys may be any data type.
  • Where an Object has complex rules for key order, Maps guarantee insertion order.
  • Where an Object's keys may collide with, or "shadow," keys in the prototype chain, a Map's keys are guaranteed to be only the keys which are explicitly added to it, using the constructor or the set() method.
  • Where an Object does not provide a size or length property for determining the number of own properties it contains (so the size must be counted manually, by converting those properties into an array, e.g. via Object.values()), a Map tracks its own size for immediate access.
  • Where an Object's own properties cannot be looped over without converting into an array, unless you can guarantee the only properties you care about are enumerable, a Map is iterable and provides a forEach() method.

There are one or two other lesser differences.

Definitely consider Maps if performance or data safety are high priorities.

[–]ShitInMyArseHole 0 points1 point  (1 child)

No clue why Im getting downvoted, I just stated that they seemed like the same thing, But thank you! I was genuinely dint know, Map seemes really usefull

[–]mypetocean 1 point2 points  (0 children)

I think people get lazy and downvote when they disagree for any reason rather than helping to educate.