编辑代码

const a = [
    {
        "id": 1,
        "value": "66b37a9488e4f",
        "text": "333",
        "children": [
            {
                "id": 1,
                "value": "66b37a9488e51",
                "text": "2313",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 2,
                "value": "66b37a9488e52",
                "text": "546464567546",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 3,
                "value": "66b37a9488e53",
                "text": "331",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 5,
                "value": "66b37a9488e54",
                "text": "13531315",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 6,
                "value": "66b37a9488e55",
                "text": "535434",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 7,
                "value": "66b37a9488e56",
                "text": "41562156",
                "shipping_id": 1,
                "children": []
            },
            {
                "id": 9,
                "value": "66b37a9488e57",
                "text": "888888",
                "shipping_id": 1,
                "children": []
            }
        ]
    },
    {
        "id": 2,
        "value": "66b37a9488e58",
        "text": "354534",
        "children": [
            {
                "id": 4,
                "value": "66b37a9488e59",
                "text": "45645",
                "shipping_id": 2,
                "children": []
            },
            {
                "id": 8,
                "value": "66b37a9488e5a",
                "text": "1111",
                "shipping_id": 2,
                "children": [
                     {
                "id": 11,
                "value": "66b37a9488aas",
                "text": "123",
                "shipping_id": 2,
                "children": []
            }
                ]
            }
        ]
    }
]
function findTextByValue(arr, targetValue) {
    for (let item of arr) {
        // 检查当前项的 value 是否匹配
        if (item.value === targetValue) {
            return item.text; // 找到匹配,返回对应的 text
        }
        
        // 如果有子项,递归查找
        if (item.children && item.children.length > 0) {
            const result = findTextByValue(item.children, targetValue);
            if (result) {
                return `${item.text} / ${result}`; // 返回路径
            }
        }
    }
    return null; // 如果没有找到,返回 null
}

// 示例调用
const targetValue = "66b37a9488aas"; // 要查找的 value
const textPath = findTextByValue(a, targetValue);
console.log(textPath); // 输出: "333 -> 546464567546"

// console.log(findPalletNumIdByValue(a, '66b37a9488e5a'))