1

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!

2 Answers2

0

You could use Array#some and return on find.

function func(data, find) {
    return Object.keys(data).some((k) => {      // return result of iteration
        iteration++;

        if (data[k].id === find) {
            console.log("found " + data[k]['id']);
            //this.call();
            return true;                        // return on direct found
        }

        if (Array.isArray(data[k].list)) {
            return func(data[k].list, find);    // return result of nested search
        }
    });
}

var 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: "#" }],
    iteration = 0,
    find = "1235235";

func(data, find);
console.log(iteration);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You could also use a for .. of loop that already supports breaking:

var 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) {
    for (const k of Object.keys(data)) {
        iteration++;

        if(data[k]['id'] === find) {
            console.log("found " + data[k]['id'] + " in iteration " + iteration);
            //this.call();
            break;
        }

        if(data[k]['list'] !== undefined) {
            return func(data[k]['list'], find);
        }
    };

    return false;
}

func(data, find);
kraf
  • 1,269
  • 12
  • 21