In this function I'm trying to find a specific item inside a JSON string. I can find the item but the issue is that once the item is found I want to break the forloop.
I'm using a recursive function because the item I'm looking for might be several levels deep down the tree.
I've tried break, break with label and several other solutions I've found.
As I'm understanding it it has something to do that I'm not in the loop when I'm trying to break but rather inside a 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);
}
});
}
Thanks in advance!