{"id":6592,"date":"2026-05-19T15:27:32","date_gmt":"2026-05-19T06:27:32","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=6592"},"modified":"2026-11-19T15:27:47","modified_gmt":"2026-11-19T06:27:47","slug":"javascript-object-groupby-array","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/6592\/","title":{"rendered":"JavaScript Object.groupBy()\u3067\u914d\u5217\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b"},"content":{"rendered":"<p>\u914d\u5217\u306e\u8981\u7d20\u3092\u30ab\u30c6\u30b4\u30ea\u3084\u72b6\u614b\u3054\u3068\u306b\u307e\u3068\u3081\u305f\u3044\u3068\u304d\u3001JavaScript\u306eObject.groupBy()\u3092\u4f7f\u3046\u3068\u3001reduce()\u3067\u96c6\u8a08\u7528\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u624b\u4f5c\u308a\u3059\u308b\u3088\u308a\u77ed\u304f\u66f8\u3051\u307e\u3059\u3002\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u8fd4\u3057\u305f\u30ad\u30fc\u3054\u3068\u306b\u914d\u5217\u304c\u5206\u304b\u308c\u308b\u306e\u3067\u3001\u5546\u54c1\u30ab\u30c6\u30b4\u30ea\u3001\u30bf\u30b9\u30af\u72b6\u614b\u3001\u65e5\u4ed8\u30b0\u30eb\u30fc\u30d7\u306a\u3069\u306e\u6574\u7406\u306b\u4f7f\u3048\u307e\u3059\u3002\u307e\u305a\u306f\u57fa\u672c\u5f62\u3092\u7406\u89e3\u3057\u3001\u623b\u308a\u5024\u304c\u901a\u5e38\u306eObject\u3068\u306f\u5c11\u3057\u9055\u3046\u70b9\u307e\u3067\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>Object.groupBy()\u3068\u306f\uff1f<\/h2>\n<p>Object.groupBy()\u306f\u3001\u914d\u5217\u306a\u3069\u306eiterable\u3092\u9806\u756a\u306b\u51e6\u7406\u3057\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u8fd4\u3057\u305f\u30ad\u30fc\u3054\u3068\u306b\u8981\u7d20\u3092\u307e\u3068\u3081\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u9759\u7684\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002MDN\u3067\u306f2024\u5e743\u6708\u4ee5\u964d\u3001\u4e3b\u8981\u30d6\u30e9\u30a6\u30b6\u30fc\u3067\u5e83\u304f\u5229\u7528\u3067\u304d\u308b\u6a5f\u80fd\u3068\u3057\u3066\u6848\u5185\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1agroupBy<\/strong><br \/>\u8907\u6570\u306e\u8981\u7d20\u3092\u300c\u540c\u3058\u6761\u4ef6\u306e\u30b0\u30eb\u30fc\u30d7\u300d\u3078\u5206\u3051\u308b\u51e6\u7406\u3067\u3059\u3002Object.groupBy()\u3067\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u623b\u308a\u5024\u304c\u30b0\u30eb\u30fc\u30d7\u540d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<\/blockquote>\n<hr>\n<h2>\u6700\u5c0f\u4f8b\uff1a\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306b\u914d\u5217\u3092\u5206\u3051\u308b<\/h2>\n<pre><code>const items = [\r\n  { name: &quot;Apple&quot;, category: &quot;fruit&quot; },\r\n  { name: &quot;Hammer&quot;, category: &quot;tool&quot; },\r\n  { name: &quot;Banana&quot;, category: &quot;fruit&quot; },\r\n];\r\n\r\nconst grouped = Object.groupBy(items, (item) =&gt; item.category);\r\n\r\nconsole.log(grouped.fruit);\r\n\/\/ Apple \u3068 Banana \u306e2\u4ef6<\/code><\/pre>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u5404\u8981\u7d20\u306b\u3064\u3044\u3066\u547c\u3070\u308c\u3001\u3053\u3053\u3067\u306fcategory\u306e\u5024\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002\u540c\u3058\u6587\u5b57\u5217\u3092\u8fd4\u3057\u305f\u8981\u7d20\u306f\u540c\u3058\u914d\u5217\u3078\u307e\u3068\u307e\u308a\u307e\u3059\u3002<\/p>\n<hr>\n<h2>reduce()\u3067\u66f8\u304f\u5834\u5408\u3068\u306e\u9055\u3044<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u89b3\u70b9<\/th>\n<th>Object.groupBy()<\/th>\n<th>reduce()<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u76ee\u7684<\/td>\n<td>\u30b0\u30eb\u30fc\u30d7\u5316\u306b\u7279\u5316<\/td>\n<td>\u96c6\u8a08\u3084\u5909\u63db\u5168\u822c<\/td>\n<\/tr>\n<tr>\n<td>\u8a18\u8ff0\u91cf<\/td>\n<td>\u77ed\u304f\u306a\u308a\u3084\u3059\u3044<\/td>\n<td>\u521d\u671f\u5024\u3084\u5206\u5c90\u3092\u81ea\u5206\u3067\u66f8\u304f<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u7531\u5ea6<\/td>\n<td>\u30b0\u30eb\u30fc\u30d7\u5358\u4f4d\u306e\u914d\u5217\u3092\u4f5c\u308b\u7528\u9014\u5411\u304d<\/td>\n<td>\u4ef6\u6570\u96c6\u8a08\u3084\u8907\u96d1\u306a\u5f62\u3078\u306e\u5909\u63db\u306b\u3082\u5411\u304f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code>const grouped = items.reduce((result, item) =&gt; {\r\n  const key = item.category;\r\n  result[key] ??= [];\r\n  result[key].push(item);\r\n  return result;\r\n}, {});<\/code><\/pre>\n<p>\u5358\u7d14\u306b\u300c\u540c\u3058\u6761\u4ef6\u3054\u3068\u306e\u914d\u5217\u3078\u5206\u3051\u308b\u300d\u306a\u3089Object.groupBy()\u304c\u8aad\u307f\u3084\u3059\u304f\u3001\u4ef6\u6570\u3060\u3051\u6570\u3048\u308b\u306a\u3069\u5225\u306e\u5f62\u3078\u96c6\u8a08\u3057\u305f\u3044\u5834\u5408\u306freduce()\u304c\u5411\u304f\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u6761\u4ef6\u5f0f\u306e\u7d50\u679c\u3067\u30b0\u30eb\u30fc\u30d7\u3092\u4f5c\u308b<\/h2>\n<pre><code>const tasks = [\r\n  { title: &quot;HTML&quot;, done: true },\r\n  { title: &quot;CSS&quot;, done: false },\r\n  { title: &quot;JavaScript&quot;, done: false },\r\n];\r\n\r\nconst grouped = Object.groupBy(tasks, (task) =&gt;\r\n  task.done ? &quot;done&quot; : &quot;todo&quot;\r\n);\r\n\r\nconsole.log(grouped.todo.length); \/\/ 2<\/code><\/pre>\n<p>\u5143\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u305d\u306e\u307e\u307e\u30ad\u30fc\u306b\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u6761\u4ef6\u5f0f\u306e\u7d50\u679c\u3068\u3057\u3066done\u3084todo\u306e\u3088\u3046\u306a\u6587\u5b57\u5217\u3092\u8fd4\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u623b\u308a\u5024\u306fnull prototype\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n<p>ECMAScript\u4ed5\u69d8\u3067\u306f\u3001Object.groupBy()\u306e\u623b\u308a\u5024\u306fObject.prototype\u3092\u7d99\u627f\u3057\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001grouped.hasOwnProperty(&#8230;)\u306e\u3088\u3046\u306a\u901a\u5e38\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u524d\u63d0\u306b\u3057\u306a\u3044\u65b9\u304c\u5b89\u5168\u3067\u3059\u3002<\/p>\n<pre><code>const grouped = Object.groupBy(items, (item) =&gt; item.category);\r\n\r\nconsole.log(Object.getPrototypeOf(grouped)); \/\/ null\r\nconsole.log(Object.hasOwn(grouped, &quot;fruit&quot;)); \/\/ true<\/code><\/pre>\n<p>\u5b58\u5728\u78ba\u8a8d\u306b\u306fObject.hasOwn()\u306e\u3088\u3046\u306a\u65b9\u6cd5\u3092\u4f7f\u3046\u3068\u3001prototype\u306e\u6709\u7121\u306b\u5de6\u53f3\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<h2>\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u3082\u8981\u7d20\u306f\u30b3\u30d4\u30fc\u3055\u308c\u306a\u3044<\/h2>\n<p>\u30b0\u30eb\u30fc\u30d7\u5185\u306e\u914d\u5217\u306b\u5165\u308b\u8981\u7d20\u306f\u3001\u5143\u306e\u914d\u5217\u306b\u3042\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u540c\u3058\u53c2\u7167\u3067\u3059\u3002\u6df1\u3044\u30b3\u30d4\u30fc\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<pre><code>const grouped = Object.groupBy(items, (item) =&gt; item.category);\r\n\r\ngrouped.fruit[0].name = &quot;Green Apple&quot;;\r\nconsole.log(items[0].name); \/\/ Green Apple<\/code><\/pre>\n<p>\u30b0\u30eb\u30fc\u30d7\u5316\u5f8c\u306e\u8981\u7d20\u3092\u5909\u66f4\u3059\u308b\u3068\u5143\u914d\u5217\u5074\u306b\u3082\u5f71\u97ff\u3057\u307e\u3059\u3002\u5143\u30c7\u30fc\u30bf\u3092\u5909\u3048\u305f\u304f\u306a\u3044\u51e6\u7406\u3067\u306f\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u8981\u7d20\u3092\u30b3\u30d4\u30fc\u3057\u3066\u304b\u3089\u7de8\u96c6\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u3046\u307e\u304f\u4f7f\u3048\u306a\u3044\u3068\u304d\u306e\u78ba\u8a8d\u30dd\u30a4\u30f3\u30c8<\/h2>\n<ul>\n<li>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u6bce\u56de\u3069\u306e\u30ad\u30fc\u3092\u8fd4\u3057\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3059\u308b\u3002<\/li>\n<li>\u30ad\u30fc\u540d\u304cundefined\u3084\u610f\u56f3\u3057\u306a\u3044\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3044\u306a\u3044\u304b\u78ba\u8a8d\u3059\u308b\u3002<\/li>\n<li>\u623b\u308a\u5024\u304cnull prototype\u3067\u3042\u308b\u3053\u3068\u3092\u7406\u89e3\u3057\u3001hasOwnProperty\u3092\u76f4\u63a5\u547c\u3070\u306a\u3044\u3002<\/li>\n<li>\u53e4\u3044\u5b9f\u884c\u74b0\u5883\u3092\u5bfe\u8c61\u306b\u3059\u308b\u5834\u5408\u306fObject.groupBy()\u5bfe\u5fdc\u72b6\u6cc1\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/li>\n<li>\u30b0\u30eb\u30fc\u30d7\u5316\u3060\u3051\u3067\u306a\u304f\u5408\u8a08\u5024\u306a\u3069\u3092\u4f5c\u308a\u305f\u3044\u5834\u5408\u306freduce()\u3084\u5225\u51e6\u7406\u3082\u6bd4\u8f03\u3059\u308b\u3002<\/li>\n<\/ul>\n<h2>\u3088\u304f\u3042\u308b\u8cea\u554f\u3068\u307e\u3068\u3081<\/h2>\n<h3>Object.groupBy()\u306f\u914d\u5217\u5c02\u7528\u3067\u3059\u304b\uff1f<\/h3>\n<p>\u914d\u5217\u3092\u542b\u3080iterable\u3092\u53d7\u3051\u53d6\u308c\u307e\u3059\u3002\u5b9f\u52d9\u3067\u306f\u914d\u5217\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u4f7f\u3044\u65b9\u304c\u5206\u304b\u308a\u3084\u3059\u3044\u51fa\u767a\u70b9\u3067\u3059\u3002<\/p>\n<h3>\u6570\u5024\u3092\u8fd4\u3057\u305f\u3089\u3069\u3046\u306a\u308a\u307e\u3059\u304b\uff1f<\/h3>\n<p>Object.groupBy()\u3067\u306f\u30b0\u30eb\u30fc\u30d7\u306e\u30ad\u30fc\u3068\u3057\u3066\u30d7\u30ed\u30d1\u30c6\u30a3\u30ad\u30fc\u3078\u5909\u63db\u3055\u308c\u307e\u3059\u3002\u8aad\u307f\u3084\u3059\u3055\u306e\u305f\u3081\u3001\u610f\u5473\u306e\u5206\u304b\u308b\u6587\u5b57\u5217\u3092\u8fd4\u3059\u8a2d\u8a08\u304c\u6271\u3044\u3084\u3059\u3044\u3067\u3059\u3002<\/p>\n<h3>reduce()\u306f\u3082\u3046\u4e0d\u8981\u3067\u3059\u304b\uff1f<\/h3>\n<p>\u4e0d\u8981\u306b\u306f\u306a\u308a\u307e\u305b\u3093\u3002Object.groupBy()\u306f\u30b0\u30eb\u30fc\u30d7\u5316\u306b\u7279\u5316\u3057\u3066\u304a\u308a\u3001\u5408\u8a08\u30fb\u5909\u63db\u30fb\u8907\u96d1\u306a\u96c6\u8a08\u3067\u306freduce()\u304c\u9069\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a3\u301c5\u4ef6\u306e\u5c0f\u3055\u306a\u914d\u5217\u3067category\u3084status\u3092\u30ad\u30fc\u306b\u5206\u3051\u3001console.log()\u3067\u623b\u308a\u5024\u3092\u78ba\u8a8d\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"featured_media":6596,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/6592"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/6596"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=6592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}