JavaScript groupBy 不是函数

JavaScript groupBy is not a functio

我想用类型名称对我的数据进行分组

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 );

上面出现错误,因为 groupBy 不是函数

JavaScript 数组没有 groupBy 方法 。请参阅 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 直接使用 core-js。)