sanjar-notes / web_dev_fundamentals

The basics - HTML, CSS, Javascript and more
https://sanjar-notes.github.io/web_dev_fundamentals/
3 stars 0 forks source link

Object.groupBy available in browsers, polyfill needed for Node, RN #106

Open sanjarcode opened 9 months ago

sanjarcode commented 9 months ago

Object.groupBy

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy

const inventory = [
  { name: "asparagus", type: "vegetables", quantity: 5 },
  { name: "bananas", type: "fruit", quantity: 0 },
  { name: "goat", type: "meat", quantity: 23 },
  { name: "cherries", type: "fruit", quantity: 5 },
  { name: "fish", type: "meat", quantity: 22 },
];

const result = Object.groupBy(inventory, ({ type }) => type);

unfortunately, not available in Node, so React won't transpile. Can't use in React.

Works in React web Polyfill exists for React native, so might work in Node.js too.


con: no way to exclude stuff from being grouped. workaround: return null instead of a string to collect non-participants. The final object has a null key then