拼接字符串

实现输入:value = 10;输出:'11-88-1010'
let list = [{
    value:1,
    text:'11',
    children:[{
        value:2,
        text:'22',
        children:[{
            value:7,
            text:'77'
        },{
            value:3,
            text:'33'
        }]
    },{
        value:8,
        text:'88',
        children:[{
            value:9,
            text:'99'
        },{
            value:10,
            text:'1010'
        }]
    },{
        value:11,
        text:'1111',
    }]
},{
    value:4,
    text:'44',
    children:[{
        value:5,
        text:'55',
    }]
},{
    value:6,
    text:'66'
}]

function getTxt(arr = list,value = 10){
    let txtArr = [];
    let txt = '';
    function getValue(arr,value){
        console.log('arr',arr);
        for(let i = 0,len = arr.length;i < len;i++){
            let item = arr[i];
            console.log('item',item);
            txtArr.push(item.text);
            if(item.children && item.children.length){
                getValue(item.children,value);
            }else{
                if(item.value == value){
                    txt = txtArr.join('-');
                    return;
                }
            }
            txtArr.pop();
        }
    }
    getValue(arr,value);
    return txt;
}

  

posted @ 2021-03-08 22:48  671_MrSix  阅读(61)  评论(0编辑  收藏  举报