<分区>
在此函数中,我试图在 JSON 字符串中查找特定项目。我可以找到该项目,但问题是一旦找到该项目我想打破 forloop。
我正在使用递归函数,因为我正在寻找的项目可能在树的深处。
我已经尝试过 break、break with label 和我找到的其他几种解决方案。
据我了解,当我试图打破时我不在循环中而是在 map 中,它有一些事情要做。
data = '[{"id":"123","name":"Level 1 1","href":"#","list":[{"id":"1235235","name":"Level 2 1","href":"#","list":[{"id":"63463634","name":"Level 2 1","href":"#"}]}]},{"id":"79699676","name":"Level 2 1","href":"#"},{"id":"959656454","name":"Level 3 1","href":"#"},{"id":"46346346346123","name":"Level 4 1","href":"#"}]';
var iteration = 0;
const find = "1235235"
function func(data, find) {
Object.keys(data).forEach((k) => {
iteration++;
if(data[k]['id'] === find) {
console.log("found " + data[k]['id']);
this.call();
break iteration;
}
if(data[k]['list'] !== undefined) {
this.func(data[k]['list'], find);
}
});
}
提前致谢!