Git Product home page Git Product logo

Comments (16)

panzerdp avatar panzerdp commented on June 25, 2024 1

Yet another great post DP with new info and clear exp !!

Thanks @kishoreandra!

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on June 25, 2024 1

The benefit of Map is that it accepts any data type as a key, but the plain object is limited to strings only.

As I know, a plain object is limited to strings and symbols, am I right?

Strictly, you're right that strings and symbols are keys in plain objects. But I almost always use strings as keys.

But I updated the post per your suggestion. Thanks @ipovos.

from dmitripavlutin.com-comments.

aralroca avatar aralroca commented on June 25, 2024 1

This article should be updated because now they rename it to Object.groupBy: https://github.com/tc39/proposal-array-grouping

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on June 25, 2024 1

@aralroca Thanks. Post updated.

from dmitripavlutin.com-comments.

kishoreandra avatar kishoreandra commented on June 25, 2024

Yet another great post DP with new info and clear exp !!

from dmitripavlutin.com-comments.

ipovos avatar ipovos commented on June 25, 2024

The benefit of Map is that it accepts any data type as a key, but the plain object is limited to strings only.

As I know, a plain object is limited to strings and symbols, am I right?

from dmitripavlutin.com-comments.

alexbenisch avatar alexbenisch commented on June 25, 2024

Second time google directed me to your useful information, thanks for sharing and for this blog.
Alex

from dmitripavlutin.com-comments.

yusuf963 avatar yusuf963 commented on June 25, 2024

appreciate the post,
I have used node 16 and node 18 to used groupBy method on an arry and both time it throw an error saying products.groupBy is not a function
any idea? thanks

from dmitripavlutin.com-comments.

kishoreandra avatar kishoreandra commented on June 25, 2024

Hey @yusuf963, please have a look at this issue (on group for group by) and it should be used carefully until it ships and official by ES committee... https://github.com/tc39/proposal-array-grouping#why-group-and-not-groupby

from dmitripavlutin.com-comments.

o-t-w avatar o-t-w commented on June 25, 2024

Couldn't your groupToMap example just have used group() seeing as it's using strings as keys? I would find it more instructive if it had returned something that actually needed to be a Map

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on June 25, 2024

Couldn't your groupToMap example just have used group() seeing as it's using strings as keys? I would find it more instructive if it had returned something that actually needed to be a Map

@o-t-w Feel free to create a Pull Request for this post with a more meaningful example. 🎸

from dmitripavlutin.com-comments.

dotnotguy avatar dotnotguy commented on June 25, 2024

Is there a way to use this and show the output in an

  • on a web page? So, instead of showing the JSON to the user, have that JSON data in an menu or table per say

  • from dmitripavlutin.com-comments.

    o-david avatar o-david commented on June 25, 2024

    Quick question, how do I map out the grouped data?

    from dmitripavlutin.com-comments.

    panzerdp avatar panzerdp commented on June 25, 2024

    Quick question, how do I map out the grouped data?

    @o-david Can you give more details what you want to achieve?

    from dmitripavlutin.com-comments.

    wiolowan avatar wiolowan commented on June 25, 2024

    Made a test example with Object.groupBy and two Array.reduce functions, one with a body like in your article, and another reduce slightly optimized:
    https://jsperf.app/raxofi
    It appears that in Chrome 118 Object.groupBy is performing slightly slower than optimized Array.reduce, while unoptimezed Array.reduce is 1/3 slower.

    Optimization is done on resulting grouping object updating, originally you have to access group[category] THREE times:

      group[category] = group[category] ?? [];
      group[category].push(product);
    

    Instead you can write:

    group[category]?.push(product) ?? ( group[category] = [ product ] );
    

    from dmitripavlutin.com-comments.

    aiktb avatar aiktb commented on June 25, 2024

    Now is there any way to make it work with TypeScript?
    I tried adding the following option to tsconfig.json but it still reports the error Property 'groupBy' does not exist on type 'ObjectConstructor'.ts(2339).

        "target": "esnext",
        "lib": ["ESNext"],

    from dmitripavlutin.com-comments.

    Related Issues (20)

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.