{"id":11357,"date":"2024-09-19T23:32:18","date_gmt":"2024-09-19T15:32:18","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11357"},"modified":"2024-09-19T23:33:21","modified_gmt":"2024-09-19T15:33:21","slug":"js-object-groupby","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/09\/js-object-groupby\/","title":{"rendered":"\u5b9e\u7528\u7684JS\u5bf9\u8c61\u5206\u7ec4\u9759\u6001\u65b9\u6cd5Object.groupBy()"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11357\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11357<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202409-groupby.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/groupby-cover.jpg\" width=\"354\" height=\"227\" alt=\"\u5c01\u9762\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u539f\u751f\u5206\u7ec4\u65b9\u6cd5Object.groupBy()<\/h3>\n<p>\u597d\u6d88\u606f\uff0c\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u6d4f\u89c8\u5668\u539f\u751f\u7684\u9759\u6001\u65b9\u6cd5<code>Object.groupBy()<\/code>\u4e86\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0cSafari\u6d4f\u89c8\u5668\u652f\u6301\u6700\u665a\uff0c\u4eca\u5e744\u6708\u4efd\u624d\u5f00\u59cb\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/2024-09-19_203656.png\" width=\"630\" height=\"265\" alt=\"groupBy\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd9\u5c31\u610f\u5473\u7740\u6700\u665a\u660e\u5e74\uff0c\u5c31\u7b97\u4e0d\u4f7f\u7528Polyfill\u4ee3\u7801\uff0c\u4e5f\u80fd\u5728\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u8be5\u65b9\u6cd5\u4e86\u3002<\/p>\n<h4>\u4f5c\u7528<\/h4>\n<p><code>Object.groupBy()<\/code>\u53ef\u4ee5\u8ba9\u53ef\u679a\u4e3e\u5bf9\u8c61\uff0c\u6839\u636e\u67d0\u4e2a\u952e\u8fdb\u884c\u81ea\u52a8\u5206\u7ec4\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const data = [{\r\n    id: 1,\r\n    name: '\u5f20\u4e09'\r\n}, {\r\n    id: 3,\r\n    name: '\u674e\u56db'\r\n}, {\r\n    id: 4,\r\n    name: '\u738b\u4e8c'\r\n}, {\r\n    id: 2,\r\n    name: '\u5f20\u4e09'\r\n}];\r\n\r\nconst result = Object.groupBy(data, ({ name}) => name);\r\n\r\nconsole.log(result);<\/pre>\n<p>\u8f93\u5165\u7ed3\u679c\u5219\u662f\uff1a<\/p>\n<pre>{\u5f20\u4e09: Array(2), \u674e\u56db: Array(1), \u738b\u4e8c: Array(1)}\r\n\u5f20\u4e09: Array(2)\r\n0: {id: 1, name: '\u5f20\u4e09'}\r\n1: {id: 2, name: '\u5f20\u4e09'}\r\nlength: 2\r\n[[Prototype]]: Array(0)\r\n\u674e\u56db: Array(1)\r\n0: {id: 3, name: '\u674e\u56db'}\r\nlength: 1\r\n[[Prototype]]: Array(0)\r\n\u738b\u4e8c: Array(1)\r\n0: {id: 4, name: '\u738b\u4e8c'}\r\nlength: 1\r\n[[Prototype]]: Array(0)<\/pre>\n<p>\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/2024-09-19_211358.png\" width=\"368\" height=\"261\" alt=\"\u5206\u7ec4\u8f93\u51fa\u7ed3\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>Object.groupBy(items, callbackFn)<\/pre>\n<dl>\n<dt><strong>items<\/strong><\/dt>\n<dd>\u5c06\u88ab\u5206\u7ec4\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/dd>\n<dt><strong>callbackFn(element, index)<\/strong><\/dt>\n<dd>\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u7684\u51fd\u6570\u3002\u5176\u8fd4\u56de\u503c\u4f1a\u88ab\u4f5c\u4e3a\u952e\uff0c\u7528\u6765\u6307\u5411\u5206\u7ec4\u540e\u7684\u6570\u7ec4\u9879\u3002\u4f7f\u7528\u4ee5\u4e0b\u53c2\u6570\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/dd>\n<\/dl>\n<h4>Polyfill\u4ee3\u7801<\/h4>\n<p>\u5982\u679c\u8981\u517c\u5bb9\u9648\u65e7\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u4f7f\u7528\u8fd9\u6bb5JavaScript\u4ee3\u7801\uff1a<\/p>\n<pre>const hasGroup = typeof Object.groupBy === typeof undefined || typeof Array.groupToMap === typeof undefined || typeof Array.group === typeof undefined;\r\nif (!hasGroup) {\r\n  const groupBy = (arr, callback) =&gt; {\r\n    return arr.reduce((acc = {}, ...args) =&gt; {\r\n      const key = callback(...args);\r\n      acc[key] ??= []\r\n      acc[key].push(args[0]);\r\n      return acc;\r\n    }, {});\r\n  };\r\n\r\n  if (typeof Object.groupBy === typeof undefined) {\r\n    Object.groupBy = groupBy;\r\n  }\r\n\r\n  if (typeof Array.groupToMap === typeof undefined) {\r\n    Array.groupToMap = groupBy;\r\n  }\r\n\r\n  if (typeof Array.group === typeof undefined) {\r\n    Array.group = groupBy;\r\n  }\r\n}<\/pre>\n<h3>\u4e8c\u3001\u5b9e\u9645\u6848\u4f8b<\/h3>\n<p>\u6211\u5728\u67d0\u9879\u76ee\u5f00\u53d1\u4e2d\u5df2\u7ecf\u4f7f\u7528\u8fc7\u6b64API\u4e86\uff0c\u7ed9\u5927\u5bb6\u6f14\u793a\u4e0b\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p>\u5df2\u77e5\u6709\u6570\u7ec4\uff1a<\/p>\n<pre>const okrAlignList = [{\r\n    objectId: 1,\r\n    empNum: 'YW001',\r\n    empName: '\u5218\u4e00'\r\n}, {\r\n    objectId: 3,\r\n    empNum: 'YW002',\r\n    empName: '\u59da\u4e8c\u4e09'\r\n}, {\r\n    objectId: 4,\r\n    empNum: 'YW003',\r\n    empName: '\u5f20\u946b\u65ed'\r\n}, {\r\n    objectId: 2,\r\n    empNum: 'YW001',\r\n    empName: '\u5218\u4e00'\r\n}]<\/pre>\n<p>\u7136\u540e\u9875\u9762\u6e32\u67d3\u7684\u65f6\u5019\uff0c\u5982\u679c\u662f\u540c\u4e00\u4eba\uff0c\u662f\u9700\u8981\u5408\u5e76\u5c55\u793a\u7684\uff0c\u5982\u914d\u56fe\u6240\u793a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/2024-09-19_225849.png\" width=\"480\" height=\"157\" alt=\"\u4eba\u540d\u622a\u56fe\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>Object.groupBy()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6e32\u67d3\uff0c\u800c\u4e0d\u9700\u8981\u81ea\u5df1\u989d\u5916\u5199\u4e00\u4e2a\u5206\u7ec4\u65b9\u6cd5\u4e86\uff0c\u6b64\u65f6\u7684Vue\u6a21\u677f\u6e32\u67d3\u4f7f\u7528\u4e0b\u9762\u7684\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre>&lt;data \r\n  v-for=\"(value, key) in Object.groupBy(okrAlignList, obj =&gt; obj.empNum)\"\r\n  :key=\"key\"\r\n&gt;\r\n  {{ value[0].empName }}&lt;output v-if=\"value.length &gt; 1\"&gt;\r\n    ({{ value.length}})\r\n  &lt;\/output&gt;\r\n&lt;\/data&gt;<\/pre>\n<p>\u4ee3\u7801\u7b80\u6d01\u591a\u4e86\u3002<\/p>\n<h3>\u4e09\u3001\u8fd8\u6709Map.groupBy()\u65b9\u6cd5<\/h3>\n<p>\u9664\u4e86<code>Object<\/code>\u5bf9\u8c61\u6709<code>groupBy()<\/code>\u9759\u6001\u65b9\u6cd5\uff0c<code>Map<\/code>\u5bf9\u8c61\u4e5f\u6709\uff0c\u517c\u5bb9\u6027\u4e00\u81f4\uff0c\u8bed\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>const inventory = [\r\n  { name: 'asparagus', type: 'vegetables', quantity: 9 },\r\n  { name: 'bananas', type: 'fruit', quantity: 5 },\r\n  { name: 'goat', type: 'meat', quantity: 23 },\r\n  { name: 'cherries', type: 'fruit', quantity: 12 },\r\n  { name: 'fish', type: 'meat', quantity: 22 },\r\n];\r\n\r\nconst restock = { restock: true };\r\nconst sufficient = { restock: false };\r\nconst result = Map.groupBy(inventory, ({ quantity }) =&gt;\r\n  quantity &lt; 6 ? restock : sufficient,\r\n);\r\nconsole.log(result.get(restock));\r\n\/\/ [{ name: \"bananas\", type: \"fruit\", quantity: 5 }]\r\n<\/pre>\n<p><code>Map.groupBy()<\/code>\u4f7f\u7528\u7528\u5728\u5206\u7ec4\u4fe1\u606f\u4f1a\u968f\u65f6\u95f4\u53d8\u5316\u7684\u573a\u666f\u4e0b\uff0c\u56e0\u4e3a\u5373\u4f7f\u5bf9\u8c61\u88ab\u4fee\u6539\uff0c\u5b83\u4ecd\u5c06\u7ee7\u7eed\u4f5c\u4e3a\u8fd4\u56deMap\u7684\u952e\u3002<\/p>\n<p>\u5176\u4ed6\u65f6\u5019\uff0c\u6362\u6210\u4f7f\u7528<code>Object.groupBy()<\/code>\u5b9e\u73b0\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<h3>\u56db\u3001\u5662\u5566\uff0c\u7ed3\u675f\u4e86\uff0c\u5c31\u8fd9\u4e9b<\/h3>\n<p>\u597d\u4e86\uff0c\u5c31\u8fd9\u70b9\u5185\u5bb9\uff0c\u7b97\u662f\u6211\u4f17\u591a\u6587\u7ae0\u91cc\u9762\u6bd4\u8f83\u6c34\u7684\u4e00\u7bc7\u4e86\u5427\u3002<\/p>\n<p>\u6211\u4e00\u822c\u8f83\u5c11\u4ecb\u7ecd\u7eafJavaScript\u8bed\u8a00\u7684API\u5c31\u662f\u8fd9\u4e2a\u539f\u56e0\u3002<\/p>\n<p>\u5c31\u662f\u5e72\u5df4\u5df4\u7684\u8bed\u6cd5\u8fd9\u4e9b\uff0c\u90fd\u662f\u5176\u4ed6\u5730\u65b9\u90fd\u80fd\u627e\u5230\u7684\u3002<\/p>\n<p>\u4e0d\u50cfCSS\u8fd9\u4e1c\u897f\uff0c\u53ef\u4ee5\u6709\u5f88\u591a\u7cbe\u5f69\u7684\u6848\u4f8b\u793a\u610f\uff0c\u4f1a\u6709\u4f17\u591a\u884d\u751f\u7684\u8868\u73b0\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u91cd\u8981\u7684\u662f\u8ba9\u5927\u5bb6\u77e5\u9053\u6d4f\u89c8\u5668\u65b0\u652f\u6301\u4e86\u4e2a\u8fd9\u4e48\u73a9\u610f\u3002<\/p>\n<p>\u65e5\u540e\u5728\u5f00\u53d1\u9879\u76ee\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u7701\u6389\u4e9b\u4ee3\u7801\uff0c\u63d0\u9ad8\u70b9\u7f16\u7801\u901f\u5ea6\u3002<\/p>\n<p>\u597d\uff0c\u5c31\u8bf4\u8fd9\u4e9b\u5427\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/yuanyao-s.jpg\" width=\"120\" height=\"157\" alt=\"\u5143\u7476\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11357\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11357<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202409\/groupby-cover.jpg\" width=\"160\" height=\"103\" alt=\"\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u597d\u6d88\u606f\uff0c\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301Map.groupBy()\u3001Object.groupBy()\u9759\u6001\u5206\u7ec4\u65b9\u6cd5\u4e86\uff0c\u6211\u4eec\u7684JavaScript\u4ee3\u7801\u53c8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7b80\u5316\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1887,2003,800,1237,1775],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11357"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11357"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11357\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}