{"id":2103,"date":"2024-03-01T12:23:50","date_gmt":"2024-03-01T04:23:50","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=2103"},"modified":"2024-03-01T12:23:50","modified_gmt":"2024-03-01T04:23:50","slug":"%e4%bd%bf%e7%94%a8%e9%80%92%e5%bd%92%e5%b0%86%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%a0%91%e5%9e%8b%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/javascript\/2024\/03\/01\/1223502103.html","title":{"rendered":"\u4f7f\u7528\u9012\u5f52\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u6811\u578b\u7ed3\u6784"},"content":{"rendered":"<pre><code class=\"language-javascript line-numbers\">const list = [{id: 100,name: 100, pid: 10}, {id: 20, name: 20, pid: 2}, {id: 1, name: 1}, {id: 2, name: 2}, {id: 10, name: 10, pid: 1}, {id: 11, name: 11, pid: 1}]\n\nconst arrayToTree = (array, pid) =&gt; {\n    return array.reduce((res, current) =&gt; {\n        if (current.pid === pid) {\n            current.children = arrayToTree(array, current.id)\n            return res.concat(current)\n        }\n        return res;\n    }, [])\n}\n\nconsole.log('arrayToTree: ', arrayToTree(list))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>const list = [{id: 100,name: 100, pid: 10}, {id: 20, name: 20, pid: 2}, {id: 1, name: 1}, {id: 2, name: 2}, {id: 10, name: 10, pid: 1}, {id: 11, name: 11, pid: 1}] const arrayToTree = (array, pid) =&gt; { return array.reduce((res, current) =&gt; { if (current.pid === pid) { current.children = arrayToTree(array, current.id) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[248,53,438,212],"class_list":["post-2103","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-array","tag-javascript","tag-tree","tag-212"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}