{"id":108286,"date":"2020-05-31T11:36:38","date_gmt":"2020-05-31T03:36:38","guid":{"rendered":"http:\/\/4563.org\/?p=108286"},"modified":"2020-05-31T11:36:38","modified_gmt":"2020-05-31T03:36:38","slug":"%e8%bf%99%e9%a2%98%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%af%94%e8%be%83%e5%a5%bd","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=108286","title":{"rendered":"\u8fd9\u9898\u5982\u4f55\u5b9e\u73b0\u6bd4\u8f83\u597d"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8fd9\u9898\u5982\u4f55\u5b9e\u73b0\u6bd4\u8f83\u597d               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pyplayer <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u628a id \u4e00\u6837\u7684\u5408\u5e76<\/p>\n<pre><code>var array = [   { id: 1, spec: 1 },   { id: 5, spec: 2 },   { id: 2, spec: 5 },   { id: 2, spec: 10 },   { id: 1, spec: 12 },   { id: 3, spec: 12 },   { id: 4, spec: 15 },   { id: 3, spec: 16 } ] \u8f6c\u53d8\u4e3a [   { id: 1, spec: [ 1, 12 ] },   { id: 5, spec: [ 2 ] },   { id: 2, spec: [ 5, 10 ] },   { id: 3, spec: [ 12, 16 ] },   { id: 4, spec: [ 15 ] } ]  <\/code><\/pre>\n<p>\u81ea\u5df1\u7684\u601d\u8def\u4f3c\u4e4e\u5f88\u70e6 \u5927\u6982\u601d\u8def\u662f\u51fa\u73b0\u8fc7\u5f97 id \u6dfb\u52a0\u5230\u4e86 idarray \u91cc,\u7136\u540e\u8fed\u4ee3\u6570\u7ec4\u6bcf\u4e00\u9879\uff0cid \u51fa\u73b0\u8fc7\u5c31\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u6ca1\u6709\u5c31\u9009\u51fa\u90a3\u4e2a\u5bf9\u8c61\u5f80\u6570\u7ec4\u540e\u9762\u6dfb\u52a0\u4e1c\u897f<\/p>\n<pre><code>function arrayChange(array){   let idArray = []   let newArray = []   for (let index = 0; index &lt; array.length; index++) {     const element = array[index];     if (idArray.includes(element.id)) {       let curEle = newArray.filter(item =&gt; item.id === element.id)       curEle[0].spec.push(element.spec)     } else {       idArray.push(element.id)       let obj = {}       obj.id = element.id       obj.spec = []       obj.spec.push(element.spec)       newArray.push(obj)     }   }   return newArray  } console.log(arrayChange(array)) <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1805301\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ynohoahc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e11\u4e11\u5730\u5b9e\u73b0\u4e00\u4e0b.<br \/>array.reduce((total, prev) =&gt; {<br \/> let item = total.find(item =&gt; item.id === prev.id)<br \/> if(!item) {<br \/> let ret = {<br \/> id: prev.id,<br \/>  spec: [prev.spec]<br \/> }<br \/> total.push(ret)<br \/> } else {<br \/> item.spec.push(prev.spec)<br \/> }<br \/> return total<br \/> }, [])                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805302\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jmc891205 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u989d js \u6ca1\u6709 hashmap \u4e00\u7c7b\u7684\u4e1c\u897f\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805303\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jmc891205 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jmc891205 \u6bcf\u6b21\u63d2\u5165\u90fd\u8981\u904d\u5386\u90a3\u592a\u6162\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805304\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rabbbit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             function solution(arr) {<br \/>\u3000 const table =new Map();<br \/>\u3000 for (const {id, spec} of arr) {<br \/>\u3000\u3000 if (!table.has(id)) table.set(id, []);<br \/>\u3000\u3000 table.get(id).push(spec);<br \/>\u3000}<br \/>\u3000 const result = [];<br \/>\u3000 for (const [id, spec] of table.entries()) {<br \/>\u3000\u3000 result.push({id, spec})<br \/>\u3000}<br \/>\u3000 return result;<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805305\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : pyplayer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ynohoahc \u6211\u8be5\u53bb\u590d\u4e60\u4e0b reduce \u4e86\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805306\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rabbbit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             function solution(arr) {<br \/>\u3000 const table =new Map();<br \/>\u3000 for (const {id, spec} of arr) {<br \/>\u3000\u3000 if (!table.has(id)) table.set(id, []);<br \/>\u3000\u3000 table.get(id).push(spec);<br \/>\u3000}<br \/>\u3000 return [&#8230;table].map(([id, spec]) =&gt; ({id, spec}));<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805307\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : haha370104 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Object.entries(<br \/> array.reduce((previousValue, currentValue) =&gt; {<br \/> previousValue[currentValue.id] = (<br \/> previousValue[currentValue.id] || []<br \/> ).concat(currentValue.spec)<br \/> return previousValue<br \/> }, {})<br \/>).map(([id, spec]) =&gt; ({ id, spec }))<\/p>\n<p>\u8fd9\u5e94\u8be5\u662f\u6211\u80fd\u60f3\u51fa\u6765\u6700\u77ed\u7684\u5199\u6cd5\u4e86\u2026\u2026                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805308\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : pyplayer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rabbbit \u975e\u5e38\u611f\u8c22 \u6211\u53bb\u505a\u4e2a\u7b14\u8bb0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805309\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : linZ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u4e2a Map \u4e0d\u597d\u4e48\u3002\u3002\u3002rabit \u7684\u5199\u6cd5\u5c31\u597d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1805310\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loonghk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \/\/ \u624b\u75d2\uff0c\u4e3a\u4e86\u56de\u7b54\u8fd9\u4e2a\uff0c\u7279\u5730\u6ce8\u518c\u4e86\u4e2a\u8d26\u53f7<\/p>\n<p>function arrayChange(arr){<\/p>\n<p> var res = {};<\/p>\n<p> arr.forEach(d=&gt;{ if(!res[d.id]){ res[d.id]=[]; } res[d.id].push(d.spec) }); \/\/ res = {1:[1,12],2:[5,10],3:[12,16],4:[15],5:[2]}<\/p>\n<p> return Object.keys(res).map(k=&gt;({&#8216;id&#8217;:k,&#8217;spec&#8217;:res[k]}));<\/p>\n<p>}                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9898\u5982\u4f55\u5b9e\u73b0\u6bd4\u8f83\u597d \u8cc7\u6df1\u5927\u4f6c : &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108286"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108286"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108286\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}