{"id":53405,"date":"2024-12-03T16:54:23","date_gmt":"2024-12-03T08:54:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53405\/"},"modified":"2024-12-03T16:54:23","modified_gmt":"2024-12-03T08:54:23","slug":"%e5%a6%82%e4%bd%95%e5%90%88%e5%b9%b6%e6%95%b0%e7%bb%84%e4%b8%adid%e7%9b%b8%e5%90%8c%e7%9a%84%e6%95%b0%e6%8d%ae%ef%bc%8c%e5%b9%b6%e6%8c%89type%e7%b1%bb%e5%9e%8b%e5%88%86%e5%88%ab%e7%94%9f%e6%88%90jine","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53405\/","title":{"rendered":"\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732930254674a6ace554d3.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u6570\u7ec4\u4e2d id \u76f8\u540c\u7684\u6570\u636e\u5408\u5e76\u4e3a\u5bf9\u8c61\uff1f<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 list\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u5305\u542b id\u3001jine \u548c type \u5c5e\u6027\u3002\u8981\u6c42\u5c06\u6570\u7ec4\u4e2d id \u76f8\u540c\u7684\u6570\u636e\u5408\u5e76\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u65b0\u7684\u5bf9\u8c61\u5e94\u5177\u6709 id \u53ca\u4e0d\u540c type \u5bf9\u5e94\u7684 jine \u5c5e\u6027\u3002<\/p>\n<p><strong>\u539f\u6570\u636e\uff1a<\/strong><\/p>\n<pre>const list = [\n    {\n        id: \"202301\",\n        jine: 23,\n        type: \"\u665a\u9910\"\n    },\n    {\n        id: \"202301\",\n        jine: 87.5,\n        type: \"\u65e9\u9910\"\n    },\n    {\n        id: \"202301\",\n        jine: 1065.5,\n        type: \"\u4e2d\u9910\"\n    },\n    {\n        id: \"202302\",\n        jine: 10,\n        type: \"\u665a\u9910\"\n    },\n    {\n        id: \"202302\",\n        jine: 181.5,\n        type: \"\u65e9\u9910\"\n    },\n    {\n        id: \"202302\",\n        jine: 633.5,\n        type: \"\u4e2d\u9910\"\n    }\n];<\/pre>\n<p><strong>\u671f\u671b\u6570\u636e\u683c\u5f0f\uff1a<\/strong><\/p>\n<pre>const list = [\n    {\n        id: \"202301\",\n        jine1: 87.5, \/\/ \u65e9\n        jine2: 1065.5, \/\/ \u4e2d\n        jine3: 23 \/\/ \u665a\n    },\n    {\n        id: \"202302\",\n        jine1: 181.5, \/\/ \u65e9\n        jine2: 633.5, \/\/ \u4e2d\n        jine3: 10 \/\/ \u665a\n    }\n];<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<pre>Object.entries(Object.groupBy(list, item =&gt; item.id))\n  .map(([k, v]) =&gt; ({ \n    id: k, \n    jine1: v.find(e =&gt; e.type === '\u65e9\u9910')?.jine, \n    jine2: v.find(e =&gt; e.type === '\u4e2d\u9910')?.jine, \n    jine3: v.find(e =&gt; e.type === '\u665a\u9910')?.jine \n  }))<\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 object.groupby() \u51fd\u6570\u5c06\u6570\u7ec4\u6309 id \u5206\u7ec4\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5176\u952e\u4e3a id \u503c\uff0c\u503c\u4e3a\u5177\u6709\u8be5 id \u7684\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 object.entries() \u51fd\u6570\u5c06\u5206\u7ec4\u540e\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/li>\n<li>\u4f7f\u7528 map() \u51fd\u6570\u904d\u5386\u952e\u503c\u5bf9\u6570\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u952e\u503c\u5bf9\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u3002<\/li>\n<li>\u65b0\u5bf9\u8c61\u5305\u542b\u4e00\u4e2a id \u5c5e\u6027\uff0c\u4ee5\u53ca\u4e09\u4e2a jine \u5c5e\u6027\uff08jine1\u3001jine2 \u548c jine3\uff09\uff0c\u5206\u522b\u5bf9\u5e94 \u65e9\u9910\u3001\u4e2d\u9910 \u548c \u665a\u9910 \u7c7b\u578b\u3002<\/li>\n<li>\u4f7f\u7528 find() \u51fd\u6570\u5728\u6bcf\u4e2a type \u6570\u7ec4\u4e2d\u67e5\u627e\u4e0e\u7ed9\u5b9a type \u5339\u914d\u7684\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u5176 jine \u503c\u3002<\/li>\n<\/ol>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u4e3a\u4ec0\u4e48body\u8bbe\u7f6eflex\u540e\uff0c\u5b50\u5143\u7d20.outer\u65e0\u6cd5\u4e0a\u4e0b\u5de6\u53f3\u5c45\u4e2d\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f \u5b66\u4e60\u6587\u7ae0\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5408\u5e76\u6570\u7ec4\u4e2dID\u76f8\u540c\u7684\u6570\u636e\uff0c\u5e76\u6309type\u7c7b\u578b\u5206\u522b\u751f\u6210jine\u5c5e\u6027\uff1f\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60\u6587\u7ae0\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 \u5982\u4f55\u5c06\u6570\u7ec4\u4e2d id \u76f8\u540c\u7684\u6570\u636e\u5408\u5e76\u4e3a\u5bf9\u8c61\uff1f \u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 list\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u5305\u542b id\u3001jine \u548c type \u5c5e\u6027\u3002\u8981\u6c42\u5c06\u6570\u7ec4\u4e2d id \u76f8\u540c\u7684\u6570\u636e\u5408\u5e76\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u65b0\u7684\u5bf9\u8c61\u5e94\u5177\u6709 id \u53ca\u4e0d\u540c type \u5bf9\u5e94\u7684 jine \u5c5e\u6027\u3002 \u539f\u6570\u636e\uff1a const list = [ { id: &#8220;202301&#8221;, jine: 23, type: &#8220;\u665a\u9910&#8221; }, { id: &#8220;202301&#8221;, jine: 87.5, type: &#8220;\u65e9\u9910&#8221; }, { id: &#8220;202301&#8221;, jine: 1065.5, type: &#8220;\u4e2d\u9910&#8221; }, { id: &#8220;202302&#8221;, jine: 10, type: &#8220;\u665a\u9910&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53405","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53405"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53405\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}