-1

I have this weird problem which makes no sense to me. What do I miss?

class AreaManager
{
    constructor()
    {
        this.areas = [];
        this.areas.push(new Area('Area1', this));
        this.areas[0].areas.push(new Area('AreaABC', this.areas[0]));
        this.areas[0].areas[0].areas.push(new Area('AreaABC123', this.areas[0].areas[0]));
        this.areas[0].areas.push(new Area('AreaDEF', this.areas[0]));
    }

    findAreaById(id, areas)
    {
        areas.forEach(a => {
            if(a.id == id)
                return a; //console.log()  prints the right Area object here
            else
                return this.findAreaById(id, a.areas);
        });
    }
}
class Area
{
    constructor(id, parent)
    {
        this.id = id;
        this.areas = [];
    }
}


var _Manager;

function InitManager()
{
    _Manager = new AreaManager();
}

function GetSomeArea()
{
    var searchID = 'AreaDEF';
    var areaObject = _Manager.findAreaById(searchID);
    console.log(areaObject); //this is always undefined
}

I'm not sure what's the problem here. Even if I try to return a simple string or a number in the if(a.id == id) I get undefined.

Hope someone can explain this. Thank you

matthias_h
  • 11,356
  • 9
  • 22
  • 40
Pascal
  • 15
  • 3

1 Answers1

-1

areas.forEach(a => { return fron the lambda function. it is not returning from parent function. Use basic for loop to break loop.

For more info, you can read my blog: how-to-break-the-loop-in-javascript

class AreaManager {
  constructor() {
    this.areas = [];
    this.areas.push(new Area("Area1", this));
    this.areas[0].areas.push(new Area("AreaABC", this.areas[0]));
    this.areas[0].areas[0].areas.push(
      new Area("AreaABC123", this.areas[0].areas[0])
    );
    this.areas[0].areas.push(new Area("AreaDEF", this.areas[0]));
  }

  findAreaById(id, areas = this.areas) {
    let merged = [];
    for (let i = 0; i < areas.length; i++) {
      if (areas[i].id == id) return areas[i];
      merged = merged.concat(areas[i].areas);
    }
    return this.findAreaById(id, merged);
  }
}
class Area {
  constructor(id, parent) {
    this.id = id;
    this.areas = [];
  }
}

var _Manager;

function InitManager() {
  _Manager = new AreaManager();
}

function GetSomeArea() {
  var searchID = "AreaDEF";
  var areaObject = _Manager.findAreaById(searchID);
  console.log(areaObject); //this is always undefined
}
InitManager();
GetSomeArea();
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • I'm still not getting it. Okay foreach can't be breaked. I updated my function to this: ` findAreaById(id, areas) { for (let i = 0; i < areas.length; i++) { console.log(areas[i].id + " == " + id); if(areas[i].id == id) return areas[i]; else return this.findAreaById(id, areas[i].areas); } } ` But now he break the recursion without looking at all elements Console.log() gives this: Area1 == AreaDEF AreaABC == AreaDEF AreaABC123 == AreaDEF – Pascal Mar 25 '20 at 17:38
  • i found bug in code. :-D so messy!! – xdeepakv Mar 25 '20 at 17:52
  • There was one data, you have 2 array element. Once you have 2 arrays, and u return from first one. So it will never go for second. so u have to merge all and look into it. Simple. – xdeepakv Mar 25 '20 at 17:53
  • Thanks, Sorry that i don't know Javascript functionalty. @Mods: thanks for closing my question and refer to a other problem which didn't helped at all. – Pascal Mar 25 '20 at 17:58