数据结构示例:
var jsonString = '[{"id":2,"label":"AAAA","children":[{"id":3,"label":"AAAA_1"}]}, {"id":24,"label":"A_01_children","children":[{"id":28,"label":"7657"}]}, {"id":19,"label":"B001"}]';
var jsonString = JSON.stringify(this.addBomOptions);
// var jsonObject = JSON.parse(jsonString);
console.log("this.addBomOptions",JSON.stringify(this.addBomOptions));
// 使用 node[node.length-1] 的值来获取对应属性
var aa = node[node.length - 1];
var jsonData = JSON.parse(jsonString);
// 遍历每个对象,调用递归函数查找目标对象
jsonData.forEach(obj => {
this.findObject(obj, aa);
})
// 递归函数,用于在对象及其children中查找特定的对象
findObject(obj, targetLabel) {
// 检查当前对象的label是否匹配目标
if (obj.label === targetLabel) {
console.log("Found in object: ", obj);
}
// 递归遍历children数组
if (obj.children) {
obj.children.forEach(function(child) {
if (child.label === targetLabel) {
console.log("Found in children: ", child);
}
// 递归遍历children的children数组
if (child.children) {
this.findObject(child, targetLabel);
}
});
}
},