• js 递归查找属性值 (根据某个属性值查找另一个属性值)


    var defaultData = [{
    text: '开发部门',
    href: '#parent1',
    tags: ['4'],
    id:3003,
    nodes: [
    {
    text: 'Child 1',
    href: '#child1',
    tags: ['2'],
    id:3004,
    nodes: [
    {
    text: 'Grandchild 1',
    href: '#grandchild1',
    tags: ['0'],
    id:3005,
    },
    {
    text: 'Grandchild 2',
    href: '#grandchild2',
    tags: ['0'],
    id:3006,
    }
    ]
    },
    {
    text: 'Child 2',
    href: '#child2',
    tags: ['0'],
    id:3007,
    }
    ]
    },
    {
    text: '产品部门',
    href: '#parent2',
    tags: ['0'],
    id:3008,
    nodes: [
    {
    text: '产品Child 1',
    href: '#child1',
    tags: ['2'],
    id:3009,
    nodes: [
    {
    text: '产品Grandchild 1',
    href: '#grandchild1',
    tags: ['0'],
    id:30010,
    }
    ]
    },
    {
    text: '产品Child 2',
    href: '#child2',
    tags: ['0'],
    id:30011,
    }
    ]
    },
    {
    text: '销售部门',
    href: '#parent3',
    tags: ['0'],
    id:30012,
    },
    {
    text: '运营部门',
    href: '#parent4',
    tags: ['0'],
    id:30013,
    },
    {
    text: '设计部门',
    href: '#parent5' ,
    tags: ['0'],
    id:30014,
    }];
    var tarText = "Grandchild 1";
    function findTargetId(dataobj,targetText){
    if(dataobj.text == targetText){
    return dataobj.id;
    }else{
    if(dataobj.nodes!=null){
    for(var i=0; i<dataobj.nodes.length; i++){
    var flagId = findTargetId(dataobj.nodes[i],targetText);
    if(flagId){
    return flagId;
    }
    }
    }
    }
    return false;
    }
    for(var i=0;i<defaultData.length;i++){
    var flag = findTargetId(defaultData[i],tarText)
    if(flag){
    result = flag;
    }

    }
    console.log(result)

  • 相关阅读:
    c# 反射应用之工厂
    UnityContainer 实现DI
    TinyMCE 的音乐插件/mp3 music insert plugin
    Django on IronPython and Windows
    说说分页
    Katze 简单的.net "ORM"框架
    Discuz!NT在64位Windows下运行的问题
    恐怖的迅雷
    基于Gettext的asp.net网站多语言解决方案
    微软是如何输掉API之战(下)
  • 原文地址:https://www.cnblogs.com/hehuiqiong/p/12712941.html
Copyright © 2020-2023  润新知