为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喵时光机 2021-03-30 13:10:53
我正在尝试flat()在Angular ts文件中使用。当我运行项目时,它给出一个错误:[question.determinativeTerms].flat不是函数!我安装了它:npm install --save array.prototype.flatfunction 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();    }, []);}
查看完整描述

2 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

npm安装软件包还不够,您需要导入并使用它。查看array.prototype.flat的文档,看来,如果要将函数添加到数组原型中,则需要在应用程序中的某处执行以下操作:


import flat from 'array.prototype.flat';

flat.shim();


查看完整回答
反对 回复 2021-04-08
  • 2 回答
  • 0 关注
  • 200 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号