javascript - 为什么我的代码会抛出错误“原型不存在”?
问题描述
如果字符串中的任何逗号分隔值在数组列表中返回true,我的请求中有字符串可以包含多个单词,我需要在数组列表中检查这些单词。我下面的代码抛出错误:
原型不存在
main.ts
const ContentList = ["check", "string", "in", "the", "array"]
private ValidateRequestArgs(str) {
let arr = new Array();
arr = ContentList;
arr.prototype.contains = function(str) {
return this.indexOf(str) > -1;
};
}
this.validateRequestArgs("check,string");
解决方案
您要解决的问题需要以下功能。但我不确定这是您想要的,因为它与您编写的函数无关。
private ValidateRequestArgs(str) {
return ContentList.some(w => str.indexOf(',' + w + ',') > -1);
}
推荐阅读
- git - 在视觉工作室团队资源管理器中提交时,它显示不同的帐户不是我的
- php - 删除 _ 和 0 的简单正则表达式
- r - r 中的子集函数返回 o 行,原因不明
- c - Valgrind 在 -g 文件中看不到行
- javascript - Css - 添加使浏览器忽略我的动画
- node.js - 在调试模式期间端口 5000 已在使用中
- c - 是否可以从函数返回结构数组?
- postgresql - 如何使用卷在 docker 容器中获取主机 postgres 数据?
- ios - 如何计算内部有stackview的自定义单元格的大小?
- css - 为什么 pull-right 类在 bootstrap 版本 4.1.0 上不起作用?