簡體   English   中英

JavaScript-如何在數組中遞歸地查找元素並返回其路徑?

[英]JavaScript- How to find an element and return its path in an array recursively?

數據結構看起來像這樣

[{id: 1,
  items:[{ id: 2,
            items: [ id: 3]
           },

          { id: 4,
           items:[id: 5]
          }]
},
{
id: 6,
items:[{ id: 7,
         items: [ id: 8]
       },

       {id: 9,
        items:[id: 10]
       }]
}]

我想通過它的id找到一個元素,並返回它的路徑,包括它的父路徑

例如,如果id=10 ,則id=10的路徑為6.9.10

findId(array, id, path,pathList){

        array.map(i => {

            if(i.items == undefined){
                return;
            }


            path = path + '.' +item.id;
            pathList.push(path);

            if(i.id == id){
                return pathList;
            }else{
                pathList.pop();
                this.findId(i.fields, id, path, pathList);

            }
        })

    }

我的問題是,如何找到id=10並返回一個包含[6, 6.9, 6.9.10]的數組

提前致謝!

你可以按照我的方法:

 var obj = [{id: 1, items:[{ id: 2, items: [{id: 3}] }, { id: 4, items:[{id: 5}] }] }, { id: 6, items:[{ id: 7, items: [{id: 8}] }, {id: 9, items:[{id: 10}] }] }]; function searchId(object, index_to_find){ //first, add depth (of each element) to array items var depths = object; //structure of path = [[0,length_1st],[1,length_2nd],[2,length_3rd],[3,length_4th],...,[last,length_last]] var path = []; //for first value of path path.push([0,depths.length]); //test to add depth for depths: depths.map(function add_depth(current){ current['depth'] = path[path.length-1][0]; if(current.items){ //continue to array items path.push([path[path.length-1][0]+1,current.items.length]); current.items.map(add_depth); }else{ //get back of path while(path.length>1 && path[path.length-1][1]<2){ path.pop(); } //decrease length path[...[x,length]] path[path.length-1][1]--; }; }); //console.log(depths); var path_result = []; var flagFound = false; depths.findIndex(function find_id(current,index){ if (flagFound){ return; }; if(current.id===index_to_find){ //finish at here path_result[current.depth] = index; flagFound = true; return; }else{ if(current.items){ path_result[current.depth] = index; current.items.findIndex(find_id); }; }; }); if(!flagFound) return undefined; //console.log(path_result); var self_items; var path_end = ""; var result_end = []; if(path_result){ for(i=0;i<path_result.length;i++){ if(i==0){ let temp_id = object[path_result[i]].id; path_end = path_end + temp_id; result_end.push(path_end); self_items = object[path_result[i]].items; }else if(i==path_result.length-1){ let temp_id = self_items[path_result[i]].id; path_end = path_end + "." + temp_id; result_end.push(path_end); }else{ let temp_id = self_items[path_result[i]].id; path_end = path_end + "." + temp_id; result_end.push(path_end); self_items = self_items[path_result[i]].items; }; } }; //console.log(path_end); return result_end; }; console.log(searchId(obj,10)); console.log(searchId(obj,8));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM