Home > Software engineering >  JavaScript groupBy is not a functio
JavaScript groupBy is not a functio

Time:05-11

i want to group my Data with the Name of type

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 }          
       let result = inventory.groupBy( ({ type }) => type );

on above getting error as groupBy is not a functio

CodePudding user response:

JavaScript arrays don't have a groupBy method yet. See MDN's documentation. It's in nightly Firefox builds at the moment and that's all. The proposal is Stage 3 so it'll be landing in JavaScript engines before too long, but in the meantime you have to polyfill it. There are various ways to polyfill it, for instance via core-js. (Babel used to do polyfilling, but now recommends using core-js directly.)

  • Related