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
filter()
Also check: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… but this is in an experimental stage. – Minutemangroup
. – Barbyreduce
– Barby