Maps vs. Objects in JavaScript - What's the Difference?

Поділитися
Вставка
  • Опубліковано 25 січ 2025

КОМЕНТАРІ •

  • @rmnkot
    @rmnkot Рік тому +12

    Also it's possible to use Object.entries() for your loop case and get the benefit of destructuring as for the Map example
    for (let [k,v] of Object.entries(o)) {
    console.log(k, '=>', v)
    }

  • @nicolashumbert8344
    @nicolashumbert8344 11 місяців тому +1

    5:20 I'm currently exploring solutions to create a huge booking calendar and I've tried using dayjs objects as keys (with data held by any date as value) and it works wonders so far.

  • @danielnadar75
    @danielnadar75 Рік тому +1

    You are amazing brother! Thanks for sharing for knowledge with us! I am surprised that this channel is so underrated! I am taking your rust course right now and it's one of the best and I am going to share it with my team as the only playlist they ever need to learn rust! Thanks again! Keep up the amazing work!

    • @hollam224
      @hollam224 Рік тому +1

      same here wonder why this page is underrated

  • @samuelmoncarey7183
    @samuelmoncarey7183 10 місяців тому +1

    To stringify a Map jou can create an Object from the Map's entries (it might only work with stringy keys)
    console.log(JSON.stringify(Object.fromEntries(personMap.entries())));

  • @dennisorbison7318
    @dennisorbison7318 Рік тому +8

    Also the thing with Maps, you are guaranteed order in the order of insertion. you wont get that with objects.

  • @montebont
    @montebont 9 місяців тому +1

    IMHO Maps work best for data structures with 2 or more dimensions.
    In the example above it would be difficult to have an indexed collection of PersonObjects because name is not suitable as a primary key..
    I use Maps for in-memory databases with unique keys that link to rows in a form. Keys are generally Base36(time()) which make them unique

  • @angryman9333
    @angryman9333 Рік тому

    I love JS and ur videos

  • @moe9647
    @moe9647 Рік тому

    you need to give practical examples ,about using Maps , use cases . Very cool video thanks mate

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj Рік тому

      when you're dealing with loads of key-value data, hash maps... Maps Are Iterable , Objects are not(so easily)
      Maps can be merged with arrays and converted to arrays... built in .size method
      it has downside too tho, no native method for serialization and parsing...
      const shoppintCart = [
      { price: 10, amount: 1 },
      { price: 15, amount: 3 },
      { price: 20, amount: 2 },
      ]
      // original code
      shoppintCart.reduce(
      (accumulator, currentItem) => {
      return {
      totalItems: accumulator.totalItems + currentItem.amount,
      totalPrice:
      accumulator.totalPrice + currentItem.amount * currentItem.price,
      }
      },
      { totalItems: 0, totalPrice: 0 } //initial value object
      )
      // { totalItems: 6, totalPrice: 45 }
      // with map
      shoppintCart.reduce(
      (accumulator, currentItem) => {
      accumulator.set('totalItems', accumulator.get('totalItems') + currentItem.amount);
      accumulator.set('totalPrice', accumulator.get('totalPrice') + currentItem.price);
      return accumulator;
      },
      new Map([['totalItems', 0], ['totalPrice', 0]])
      )
      // { 'totalItems' => 6, 'totalPrice' => 45 }

  • @maccodifavole
    @maccodifavole Рік тому

    how do Maps work with typescript? and can be they turned into JSON?

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj Рік тому +1

      maps don't have native method for serialization and parsing
      U have to use Array method.
      const map1 = new Map([
      [1, 2],
      [2, 3],
      [4, 5]
      ]);

      const arr = Array.from(map1);
      const serialized = JSON.stringify(arr);

  • @souravrouth5333
    @souravrouth5333 Рік тому

    Explained very well but it would have been more better if you could atleast show an example of map's usecase.. although loved the explanation ❤

  • @neutralitat2570
    @neutralitat2570 10 місяців тому

    JSON.stringify(Object.fromEntries(yourMap))

  • @goldmikanik8274
    @goldmikanik8274 Рік тому

    Well I didn't see the benefits of the Map, actually I am more convinced to use regular Objects now 😅

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj Рік тому

      come back after you have to loop over deeply nested objects :D