TypeError: products.groupBy is not a function
Asked Answered
T

5

7

I was reading that JS recently introduced a new array method groupBy to manipulate an array such

const products = [
  { name: 'apples', category: 'fruits' },
  { name: 'oranges', category: 'fruits' },
  { name: 'potatoes', category: 'vegetables' }
];

the that this code

const groupByCategory = products.groupBy(product => {
  return product.category;
});
console.log(groupByCategory)

should produce this output

// {
//   'fruits': [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ],
//   'vegetables': [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// }

however, I have used node 18.7.0 but the log saying TypeError: products.groupBy is not a function My question does this method works on any of node version?

Note that I don't to use reduce

Torsion answered 5/8, 2022 at 17:8 Comment(5)
you can use filter() Also check: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… but this is in an experimental stage.Minuteman
are you using a polyfill. anyway even with a polyfill it is now group.Barby
Uncaught TypeError: products.group is not a functionTorsion
So it's not supported by Node.js (yet).Dogmatic
it is still in stage 3 and not supported by node yet. you can either use a polyfill. or lodash or just custom grouping using reduceBarby
G
12

Javascript Reduce() method to make Group By Like SQL

            const products = [
              { name: 'apples', category: 'fruits' },
              { name: 'oranges', category: 'fruits' },
              { name: 'potatoes', category: 'vegetables' }
            ];

            var result = products.reduce((x, y) => {

                (x[y.category] = x[y.category] || []).push(y);

                return x;

            }, {});

            console.log(result);

            Output:  {fruits: Array(2), vegetables: Array(1)} 
Geochronology answered 26/11, 2022 at 10:21 Comment(0)
M
3

groupBy is not a function in Node.js. You should first import it and then use it.

  • First, install: npm i core-js
  • Then, import:
    import { groupBy } from "core-js/actual/array/group-by";
    
    const groupByCategoryMap = products.groupBy(product => product.category);
    
Mize answered 11/3, 2023 at 9:28 Comment(0)
A
2

Since you are using functionality is in a testing state you must install its dependency using npm i core-js, then you can access this functionality by importing. ex

require("core-js/actual/array/group-by-to-map");
require("core-js/actual/array/group-by");

const products = [
  { name: "apples", category: "fruits" },
  { name: "oranges", category: "fruits" },
  { name: "potatoes", category: "vegetables" }
];

const groupByCategory = products.groupBy((product) => {
  return product.category;
});

const groupByCategoryMap = products.groupByToMap((product) => {
  return product.category;
});

console.log(groupByCategory);
console.log(groupByCategoryMap);

You can see this in the same example where they talk about its use Read Here, if you look at the code examples given in that forum it requires importing and installing core-js Here.

Amphitropous answered 5/8, 2022 at 17:32 Comment(3)
import 'core-js/actual' ^^^^^^ SyntaxError: Cannot use import statement outside a moduleTorsion
thanks that was very helpful, however, this should be require or at least in my case require("core-js/actual/array/group-by");Torsion
I tried switching from imports to require and it still works.Amphitropous
E
1

groupBy was indeed introduced to standard JS API. However it is the static method of the Object class. You're trying to use it as instance method.


    const groupByCategory = Object.groupBy(products, product => {
      return product.category;
    });
    console.log(groupByCategory)
    
    /* {
        "fruits": [
            {
                "name": "apples",
                "category": "fruits"
            },
            {
                "name": "oranges",
                "category": "fruits"
            }
        ],
        "vegetables": [
            {
                "name": "potatoes",
                "category": "vegetables"
            }
        ]
    } */

Esbensen answered 19/9 at 0:23 Comment(0)
R
0

Add "node": "^21.6.1" to your package.json > dependencies. This solved the issue for me.

Rejoin answered 14/6 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.