Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在typescript中递归调用方法_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 如何在typescript中递归调用方法

Javascript 如何在typescript中递归调用方法,javascript,angular,typescript,Javascript,Angular,Typescript,我想在filterArr中调用filterArr。这是我的实现 filterArr(array, search) { var result = []; array.forEach((a)=> { var temp = [], o = {}, found = false; if (a.name === search) {

我想在filterArr中调用filterArr。这是我的实现

 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;
    }

如何在没有任何错误的情况下调用filterArr方法?

您必须使用
箭头函数
来获得正确的
,因此您需要更改
数组。forEach(函数(a){
来使用`箭头函数

array.forEach((a) => {

如果您删除
this.
部分
this.filterArr(a.children,search)
,它是否显示任何警告?可能重复的是当我删除this时,它显示错误