Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript indexOf()不';行不通_Javascript_Arrays_Indexof - Fatal编程技术网

Javascript indexOf()不';行不通

Javascript indexOf()不';行不通,javascript,arrays,indexof,Javascript,Arrays,Indexof,我得到了一个字符串数组,我想检查这些字符串中是否有包含某个子字符串的 下面的代码是我需要实现的一个示例:该函数从用户处获取消息,并通过一系列URL搜索标题中包含用户消息的URL: var redditSubModule = "node"; var http = require('https'); var url = "https://www.reddit.com/r/soccerstreams/new/.json"; var i = 0; var subject = (ctx.message.

我得到了一个字符串数组,我想检查这些字符串中是否有包含某个子字符串的

下面的代码是我需要实现的一个示例:该函数从用户处获取消息,并通过一系列URL搜索标题中包含用户消息的URL:

var redditSubModule = "node";
var http = require('https');
var url = "https://www.reddit.com/r/soccerstreams/new/.json";
var i = 0;
var subject =  (ctx.message.text).substring(8);
var request = http.get(url, function(response) {
    var json = '';
    response.on('data', function(chunk) {
        json += chunk;
        });
    response.on('end', function() {
    var redditResponse = JSON.parse(json);
    redditResponse.data.children.forEach(function(child) {
        if(child.data.domain !== 'self.node') {
            url = child.data.url;
            console.log('URL : ' + url);
            if (url.indexOf(subject) > -1) {
                console.log('URL : ' + url);
            } else {
                console.log("nothing...");
            }
            i++;

            }
        }); 
    })
});
request.on('error', function(err) {
    console.log(err);
});
如果您尝试在主题中填入“USA”(因为标题中有一个带有该字符串的线程),indexOf似乎不起作用,它会打印一个“nothing…”列表
记录url的类型将字符串作为类型,所以我不知道这里发生了什么

我做错了什么?

您可以使用
array.prototype.find
array.prototype.includes

var-strs=[“aaa”、“xxx”、“yyy_-leon”];
var search=“leon”;
var found=strs.find(s=>s.includes(search));
如果(找到){
日志(“找到:”,找到);
}否则{
控制台日志(“无”);

}
给我们一个运行示例,给我们一个数组,变量substringCode看起来没问题,我想如果你做一个运行示例会更容易。你应该把let或var放在
i=0
的前面(数组[i]./*toString().*/indexOf(子字符串)对我来说似乎很奇怪…你发布的代码实际上是有效的。你怎么知道你的数组包含你认为它会包含的值?@LeoR.
.indexOf()
函数在搜索文本中找不到搜索字符串时返回
-1
。如果只是布尔检查,则返回
some