2021年flag-300+パスアルゴリズム~~


弱いアルゴリズムに直面して、ここでflagを決めて、2021年に300の+アルゴリズムの問題を実現して、この記録時間2021-01-01 23:36:55から始まります
再帰関連練習
1、commentIdが空であることは、一級が空でないことを表す.id実装ツリー構造コードは以下の通りである/** * @description * @author cq * @Date 2021-01-01 22:48:23 * @LastEditTime 2021-01-01 23:39:05 * @LastEditors cq */ // commentId _id // h1 h2 h3 const arr = [ { commentId: "98bb04175fef248a00f405f81c80d217", createTime: "2021-01-01 13:33:55", questionId: "a8831daa5fec73020126af6b2940f269", text: " 1-1 1-1-2", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "85ff8afa5fef24c401285c05361e3a60", }, { commentId: "98bb04175fef248a00f405f81c80d217", createTime: "2021-01-01 13:33:35", questionId: "a8831daa5fec73020126af6b2940f269", text: " 1-1 1-1-1", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "2424fa985fef24af0106b8f77f917199", }, { commentId: "2f6ab8515fef247600ebabef520933ce", createTime: "2021-01-01 13:33:13", questionId: "a8831daa5fec73020126af6b2940f269", text: " 3-1", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "9f2a34705fef2499012c075e4669fe44", }, { commentId: "2f6ab8515fef246400ebab0350bf130a", createTime: "2021-01-01 13:32:58", questionId: "a8831daa5fec73020126af6b2940f269", text: " 1-1", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "98bb04175fef248a00f405f81c80d217", }, { commentId: "", createTime: "2021-01-01 13:32:38", questionId: "a8831daa5fec73020126af6b2940f269", text: " ", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "2f6ab8515fef247600ebabef520933ce", }, { commentId: "", createTime: "2021-01-01 13:32:30", questionId: "a8831daa5fec73020126af6b2940f269", text: " ", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "98bb04175fef246e00f4046a439f441b", }, { commentId: "", createTime: "2021-01-01 13:32:20", questionId: "a8831daa5fec73020126af6b2940f269", text: " ", user_id: "o2ml-5c_nKI2Tf9pLBJBCdnbu5v4", _id: "2f6ab8515fef246400ebab0350bf130a", }, ] function deep(array = [], commentId = "", newArr = []) { let arr = array.filter(item => item.commentId == commentId); for (let index = 0; index < arr.length; index++) { const element = arr[index]; newArr.push({ item: element, children: [] }); // const downObj = array.find(item => item.commentId == newArr[newArr.length - 1].item._id); if (downObj) { deep(array, element._id, newArr[index].children) } else { continue } } return newArr } console.log(deep(arr, "", []), "deep")View Code