{"id":52868,"date":"2024-12-03T16:23:59","date_gmt":"2024-12-03T08:23:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52868\/"},"modified":"2024-12-03T16:23:59","modified_gmt":"2024-12-03T08:23:59","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e7%94%9f%e6%88%90%e5%a4%9a%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9a%84%e7%ac%9b%e5%8d%a1%e5%b0%94%e7%a7%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52868\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324204846742a384f2658.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u751f\u6210\u7b1b\u5361\u5c14\u79ef\uff1f<\/h2>\n<h3>\u95ee\u9898\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u7ec4\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5b83\u4eec\u7684\u6240\u6709\u5143\u7d20\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u65b0\u6570\u7ec4\u5305\u542b\u539f\u59cb\u6570\u7ec4\u4e2d\u5bf9\u5e94\u5143\u7d20\u7684\u7b1b\u5361\u5c14\u79ef\u3002<\/p>\n<h3>\u4f8b\u5b50<\/h3>\n<p>\u8ba9\u6211\u4eec\u4ee5\u4e00\u7ec4\u4ee3\u8868\u667a\u80fd\u624b\u673a\u578b\u53f7\u3001\u989c\u8272\u548c\u5b58\u50a8\u5bb9\u91cf\u7684\u6570\u7ec4\u4e3a\u4f8b\uff1a<\/p>\n<pre>let names = [\"iphone x\", \"iphone xs\"]\nlet colors = [\"\u9ed1\u8272\", \"\u767d\u8272\"]\nlet storages = [\"64g\", \"256g\"]<\/pre>\n<p>\u76ee\u6807\u662f\u751f\u6210\u4ee5\u4e0b\u683c\u5f0f\u7684\u65b0\u6570\u7ec4\uff1a<\/p>\n<pre>[\n    {\n        \"spec\": [\n            {\n                \"name\": \"iphone x\"\n            },\n            {\n                \"name\": \"\u9ed1\u8272\"\n            },\n            {\n                \"name\": \"64g\"\n            }\n        ]\n    },\n    {\n        \"spec\": [\n            {\n                \"name\": \"iphone x\"\n            },\n            {\n                \"name\": \"\u9ed1\u8272\"\n            },\n            {\n                \"name\": \"256g\"\n            }\n        ]\n    },\n    {\n        \"spec\": [\n            {\n                \"name\": \"iphone x\"\n            },\n            {\n                \"name\": \"\u767d\u8272\"\n            },\n            {\n                \"name\": \"64g\"\n            }\n        ]\n    },\n    \/\/ ...\n]<\/pre>\n<h3>\u89e3\u7b54<\/h3>\n<p>\u751f\u6210\u7b1b\u5361\u5c14\u79ef\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3002\u4ee5\u4e0b javascript \u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u6b64\u529f\u80fd\uff1a<\/p>\n<pre>let combine = function (...chunks) {\n  let res = []\n\n  let helper = function (chunkIndex, prev) {\n    let chunk = chunks[chunkIndex]\n    let isLast = chunkIndex === chunks.length - 1\n    for (let val of chunk) {\n      let cur = prev.concat({ name: val })\n      if (isLast) {\n        \/\/ \u5982\u679c\u5df2\u7ecf\u5904\u7406\u5230\u6570\u7ec4\u7684\u6700\u540e\u4e00\u9879\u4e86 \u5219\u628a\u62fc\u63a5\u7684\u7ed3\u679c\u653e\u5165\u8fd4\u56de\u503c\u4e2d\n        res.push({ spec: cur })\n      } else {\n        helper(chunkIndex + 1, cur)\n      }\n    }\n  }\n\n  \/\/ \u4ece\u5c5e\u6027\u6570\u7ec4\u4e0b\u6807\u4e3a 0 \u5f00\u59cb\u5904\u7406\n  \/\/ \u5e76\u4e14\u6b64\u65f6\u7684 prev \u662f\u4e2a\u7a7a\u6570\u7ec4\n  helper(0, [])\n\n  return res\n}\n\nconsole.log(combine(names, colors, storages))<\/pre>\n<h3>\u4ee3\u7801\u89e3\u91ca<\/h3>\n<ul>\n<li>combine \u51fd\u6570\u4f9d\u6b21\u63a5\u53d7\u9700\u8981\u7b1b\u5361\u5c14\u79ef\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u5185\u90e8\u51fd\u6570 helper \u4f7f\u7528\u9012\u5f52\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u9644\u52a0\u5230\u5f53\u524d\u7ed3\u679c\u4e2d\u3002<\/li>\n<li>\u5f53\u5904\u7406\u5230\u6700\u540e\u4e00\u4e2a\u6570\u7ec4\u65f6\uff0chelper \u51fd\u6570\u5c06\u5f53\u524d\u7ed3\u679c\u8ffd\u52a0\u5230\u7ed3\u679c\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u8c03\u7528 combine(names, colors, storages) \u5c06\u751f\u6210\u6240\u9700\u7684\u7b1b\u5361\u5c14\u79ef\u3002<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u8c03\u8bd5\u65f6\u5143\u7d20\u70b9\u51fb\u4e8b\u4ef6\u6d88\u5931\u600e\u4e48\u529e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u591a\u4e2a\u6570\u7ec4\u7684\u7b1b\u5361\u5c14\u79ef\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u5982\u4f55\u751f\u6210\u7b1b\u5361\u5c14\u79ef\uff1f \u95ee\u9898\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u7ec4\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5b83\u4eec\u7684\u6240\u6709\u5143\u7d20\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u65b0\u6570\u7ec4\u5305\u542b\u539f\u59cb\u6570\u7ec4\u4e2d\u5bf9\u5e94\u5143\u7d20\u7684\u7b1b\u5361\u5c14\u79ef\u3002 \u4f8b\u5b50 \u8ba9\u6211\u4eec\u4ee5\u4e00\u7ec4\u4ee3\u8868\u667a\u80fd\u624b\u673a\u578b\u53f7\u3001\u989c\u8272\u548c\u5b58\u50a8\u5bb9\u91cf\u7684\u6570\u7ec4\u4e3a\u4f8b\uff1a let names = [&#8220;iphone x&#8221;, &#8220;iphone xs&#8221;] let colors = [&#8220;\u9ed1\u8272&#8221;, &#8220;\u767d\u8272&#8221;] let storages = [&#8220;64g&#8221;, &#8220;256g&#8221;] \u76ee\u6807\u662f\u751f\u6210\u4ee5\u4e0b\u683c\u5f0f\u7684\u65b0\u6570\u7ec4\uff1a [ { &#8220;spec&#8221;: [ { &#8220;name&#8221;: &#8220;iphone x&#8221; }, { &#8220;name&#8221;: &#8220;\u9ed1\u8272&#8221; }, { &#8220;name&#8221;: &#8220;64g&#8221; } ] }, { &#8220;spec&#8221;: [ { &#8220;name&#8221;: &#8220;iphone x&#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-52868","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52868","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=52868"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52868\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}