1

I want to call the filterArr inside the filterArr. Here is my implementation.

 filterArr(array, search) {
        var result = [];
        array.forEach((a)=> {
            var temp = [],
                 o = {},
                found = false;

            if (a.name === search) {
                this.o.name = a.name;
                found = true;
            }
            if (Array.isArray(a.children)) {
                temp = this.filterArr(a.children, search);//***Cannot read property 'filterArr' of undefined***
                if (temp.length) {
                    this.o.children = temp;
                    found = true;
                }
            }
            if (found) {
                result.push(o);
            }
        });
        return result;
    }

How to call the filterArr method without any error?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Suraj Khanal
  • 498
  • 8
  • 26

1 Answers1

3

You have to use Arrow function to get hold on correct this, so you need to change array.forEach(function (a) { to use `Arrow function

array.forEach((a) => {
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299