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

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


npm install --save 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();

    }, []);

}


喵喵时光机
浏览 201回答 2
2回答

繁星点点滴滴

npm安装软件包还不够,您需要导入并使用它。查看array.prototype.flat的文档,看来,如果要将函数添加到数组原型中,则需要在应用程序中的某处执行以下操作:import flat from 'array.prototype.flat';flat.shim();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript