[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy [Back]  [Original]

Map.groupBy() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Map.groupBy()

Baseline 2024
Newly available

Since 2024 3, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

: Array.prototype.groupToMap() . . .

Map.groupBy() . Map .

, . Object.groupBy() .

In this article

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 }]

js
Map.groupBy(items, callbackFn)

items

Array .

callbackFn

. (object ) . .

element

index

Map .

Map.groupBy() callbackFn . . callbackFn Map.groupBy() Map . .

Map . . Map .

. Map . Object.groupBy() .

: Map Map ( ). .

Map.groupBy this . Map .

Map.groupBy()

. type quantity .

js
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 },
];

quantity < 6 restock sufficient Map.groupBy() . result Map get() .

js
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 }]

{ quantity } . quantity quantity . .

Map . . .

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

//         !
const restock2 = { restock: true };
console.log(result.get(restock2)); // undefined

Specification
ECMAScript 2027 LanguageSpecification
# sec-map.groupby


Web Proxy Viewer  |  New URL  |  Original Page