首页 > 编程语言 >JavaScript Map.groupBy All In One

JavaScript Map.groupBy All In One

时间:2023-09-02 15:55:57浏览次数:50  
标签:Map name restock JavaScript type groupBy quantity

JavaScript Map.groupBy All In One

Map.groupBy(items, callbackFn)

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

const restock = { restock: true };
const sufficient = { restock: false };
const result = Map.groupBy(inventory, ({ quantity }) =>
  quantity < 6 ? restock : sufficient,
);

console.log(result.get(restock));
// [{ name: "bananas", type: "fruit", quantity: 5 }]

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

error ❌

TypeError: Map.groupBy is not a function

image

solution

not support for now ⚠️

image

image

demos

(

标签:Map,name,restock,JavaScript,type,groupBy,quantity
From: https://www.cnblogs.com/xgqfrms/p/17673771.html

相关文章