js 生成树以及关键字搜索生成树

function main(keywords,data){
  function fn(arr){
    var flag = false;
    for(var i = 0;i <arr.length;i++){
      var item = arr[i];
      if(item.children.length > 0){
        var res = fn(item.children);
        if(res){
          item.isHidden = false;
          if(!item.open){
            item.open = true;
          }
        }
      }
      if(item.isHidden === false){
        flag =  true;
      }
    }
    return flag;
  }
  
  function deal(arr,arr1){
    arr.map(function(item){
      if(item.children.length > 0){
        deal(item.children,arr1);
      }
      delete item.children;
      arr1.push(item);
    })
  }
  
  
  function createTree (originArr, pid, indexId, parentId) {
    var tree = [];
    for (var i = 0; i < originArr.length; i++) {
      var item = originArr[i];
      if (item[parentId] == pid) {
        item.children = createTree(originArr, item[indexId], indexId, parentId);
        tree.push(item);
      }
    }
    return tree;
  }
    //var keywords = '成都镇';
    data.map(function(item,index){
    keywords = keywords.trim();
    if(keywords != ''){
      if(item.name.indexOf(keywords) > -1){
        item.isHidden = false;
      } else {
        item.isHidden = true;
      }
    }else{
      item.isHidden = false;
    }
  });
  var result = createTree(data,0,'id','parentId');
  fn(result);

  var arr1 = [];
  deal(result,arr1);
  var arr2 = arr1.filter(function(item){
    return item.isHidden == false
  });
  // console.log(arr2);
  var idArrs = [];
  var pidArrs = [];
  var arr4 = [];
  arr2.map(function(item){
    if(idArrs.indexOf(item.id) < 0 ){
      idArrs.push(item.id);
    }
    if(pidArrs.indexOf(item.parentId) < 0 ){
      pidArrs.push(item.parentId);
    }
  });
  // idArrs
 
  idArrs.map(function(item1){
    if(pidArrs.indexOf(item1)<0 && arr4.indexOf(item1) < 0){
      arr4.push(item1)
    }
  });
  for (var i = 0; i < arr4.length; i++) {
    var item = arr4[i];
    var result1 = createTree(data,item,'id','parentId');
    deal(result1,arr2)
  }
  var finalRes =  createTree(arr2,0,'id','parentId');
  return finalRes;
}
var data = [
  { id: 1, parentId: 0, name: '四川' },
  { id: 2, parentId: 0, name: '贵州' },
  { id: 3, parentId: 0, name: '云南' },
  { id: 4, parentId: 0, name: '江苏' },

  { id: 5, parentId: 1, name: '成都' },
  { id: 6, parentId: 2, name: '贵州' },
  { id: 7, parentId: 3, name: '昆明' },
  { id: 8, parentId: 4, name: '苏州' },

  { id: 9, parentId: 5, name: '成都县1' },
  { id: 10, parentId: 5, name: '成都县2' },
  { id: 11, parentId: 5, name: '成都县3' },
  { id: 12, parentId: 5, name: '成都县4' },
  { id: 13, parentId: 5, name: '成都县5' },

  { id: 14, parentId: 6, name: '贵州县1' },
  { id: 15, parentId: 6, name: '贵州县2' },
  { id: 16, parentId: 6, name: '贵州县3' },

  { id: 17, parentId: 7, name: '昆明县1' },
  { id: 18, parentId: 7, name: '昆明县2' },
  { id: 19, parentId: 7, name: '昆明县3' },

  { id: 20, parentId: 8, name: '苏州县1' },
  { id: 21, parentId: 8, name: '苏州县2' },
  { id: 22, parentId: 8, name: '苏州县3' },
  { id: 23, parentId: 8, name: '苏州县4' },

  { id: 24, parentId: 9, name: '成都镇1' },
  { id: 25, parentId: 10, name: '成都镇2' },

  { id: 26, parentId: 24, name: '成都村11' },
  { id: 27, parentId: 24, name: '成都村12' },
  { id: 28, parentId: 24, name: '成都村13' }
];
var re = main("成都镇",data);
console.log(JSON.stringify(re,null,2));

  

[
  {
    "id": 1,
    "parentId": 0,
    "name": "四川",
    "isHidden": false,
    "open": true,
    "children": [
      {
        "id": 5,
        "parentId": 1,
        "name": "成都",
        "isHidden": false,
        "open": true,
        "children": [
          {
            "id": 9,
            "parentId": 5,
            "name": "成都县1",
            "isHidden": false,
            "open": true,
            "children": [
              {
                "id": 24,
                "parentId": 9,
                "name": "成都镇1",
                "isHidden": false,
                "children": [
                  {
                    "id": 26,
                    "parentId": 24,
                    "name": "成都村11",
                    "isHidden": true,
                    "children": []
                  },
                  {
                    "id": 27,
                    "parentId": 24,
                    "name": "成都村12",
                    "isHidden": true,
                    "children": []
                  },
                  {
                    "id": 28,
                    "parentId": 24,
                    "name": "成都村13",
                    "isHidden": true,
                    "children": []
                  }
                ]
              }
            ]
          },
          {
            "id": 10,
            "parentId": 5,
            "name": "成都县2",
            "isHidden": false,
            "open": true,
            "children": [
              {
                "id": 25,
                "parentId": 10,
                "name": "成都镇2",
                "isHidden": false,
                "children": []
              }
            ]
          }
        ]
      }
    ]
  }
]

 

上一篇:java 数据拼装成树结构


下一篇:第 12 章 桥接模式