Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 如何在角度项目中使用.flat()?_Javascript_Angular - Fatal编程技术网

Javascript 如何在角度项目中使用.flat()?

Javascript 如何在角度项目中使用.flat()?,javascript,angular,Javascript,Angular,我试图在我的ts文件中使用flat()。当我运行项目时,它给出一个错误:[question.determinativeTerms].flat不是一个函数! 我安装了它: npm安装--保存array.prototype.flat function matchUserInputToDeterminativePhrases( faqs: Question[], searchPhrase: string ): Question[] { return faqs.reduce((m

我试图在我的ts文件中使用
flat()
。当我运行项目时,它给出一个错误:
[question.determinativeTerms].flat
不是一个函数! 我安装了它:

npm安装--保存array.prototype.flat

function matchUserInputToDeterminativePhrases(
    faqs: Question[],
    searchPhrase: string
): Question[] {
    return faqs.reduce((matches, question) => {
        let foundMatch = false;
        [question.determinativeTerms].flat().forEach(term => {
            if (
                !foundMatch &&
                searchPhrase &&
                searchPhrase
                .toLowerCase()
                .search(new RegExp(`\\b${term.toLowerCase()}`)) !== -1
            ) {
                foundMatch = true;
                matches.push(question);
            }
        });
        return matches.reverse();
    }, []);
}

仅npm安装软件包是不够的,您需要导入并使用它。从中可以看出,如果要将该函数添加到阵列原型中,则需要在应用程序中的某个位置执行以下操作:

import flat from 'array.prototype.flat';
flat.shim();

从angular 11和thx到现在,您可以直接使用angular内部的
flat()
方法,并在
tsconfig.json
文件中进行以下配置

“编译器选项”:{
“目标”:“es2018”,
“模块”:“es2020”,
“lib”:[“es2020”,“dom”],
}

您npm安装了它,但您是否在任何地方导入了它?是的,我尝试过,从“array.prototype.flat”导入平面;在我使用flat()方法的组件和main.ts中!