我正在尝试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();
添加回答
举报
0/150
提交
取消