script >
//分页查询数据
async function fetchData ( pagesize = 100, skip = 0)[ Show usages
const result = await db.co llection (' your_collection '). skip ( skip ). limit ( pagesj return result . data ;
ト
//构建树形结构
function buildTree ( data ){ Show usages
const map = new Map ();
const tree =[];
//将所有节点放入 map 中,以便快速查找
for ( const node of data ){
map . set ( node . id ,{... node , children :[] h ):
//构建树形结构
for ( const node of data )
if ( node . parentId === nutl ){
tree . push ( map . get ( node . id ));
h else {
const parent = map . get ( node . parentId );
if ( parent ){
parent . children . push ( map . get ( node . id ));
return tree :
//查找市点及其后
function findNodeAndChildren ( tree , targetId , depth = 0, maxDepth = 3) Show usag for ( const node of tree ){
targetid ) if ( node . id ===
if ( depth <= maxDepth )
return
id : node . id ,
name : node.name
buildTree 0
く
F 3
F 4
F 5
F 6
F 7
F 8
F 9
F 10
+
x
>
9月12日 10:19
4 S
5%
V 9
7&
8*
9(
R )运行( U ) 工具 I VCS ( S )窗口( W ) 帮助( H ) demo -51. html
38. html 49. html 50. html 51. html H 48. html H H < script >
. html
function findNodeAndChildren ( tree , targetId , depth = e , maxDepth = 3)
广
} else {
if ( node . children && node . children . tength >0)
const childResult = findNodeAndChildren ( node . children , if ( childResult )
return childResult ;
return null ;
// 云函数入口
exports . main = async ( event , context ) =>[
const { targetId = event ;
const pagesize = 100;
Let skip = o ;
Let allData =[];
//分页查询所有数据
while ( true )
const data = await fetchData ( pagesize , skip );
I
if ( data . length === 0) break ;
allData = allData.co ncat ( data );
skip += pagesize ;
//构建树形结构
const tree = buildTree ( allData );
//查找指定 ID 的节点及其后三级的数据
const result = findNodeAndChildren ( tree , targetId );
return result ;
uildTree 0