首页 > 解决方案 > 为什么我的代码会抛出错误“原型不存在”?

问题描述

如果字符串中的任何逗号分隔值在数组列表中返回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");

标签: javascriptarraystypescript

解决方案


您要解决的问题需要以下功能。但我不确定这是您想要的,因为它与您编写的函数无关。

private ValidateRequestArgs(str) {
     return ContentList.some(w => str.indexOf(',' + w + ',') > -1);
}

推荐阅读