简体   繁体   English

表格:indexOf找不到子字符串

[英]Sheets: indexOf not finding substring

在此处输入图片说明

Working in apps script I have : 在应用程序脚本中工作,我有:

var conversations = row['CONVERSATION'].split('|');
var replies = conversations.map(function(message) { //ONLY CHECKED ROWS.
  var m = message.toString();
  return m.indexOf('R:')==true;
});  

    Logger.log(conversations);
    Logger.log(replies);

When I look at the logs: 当我查看日志时:

[19-03-08 11:51:12:892 EST] [R: test3 ,  R: test3 ,  tx]
[19-03-08 11:51:12:893 EST] [false, true, false]

Why is the first element in the replies array false. 为什么replies数组中的第一个元素为false。 Shouldn't it be true? 难道不是真的吗?

Why is the first element in the replies array false. 为什么replies数组中的第一个元素为false。 Shouldn't it be true? 难道不是真的吗?

So if the indexOf matches first element you get index as 0 and your condition is checking for true values but 0 is not so it is returning you false. 因此,如果indexOf匹配第一个元素,则将index设置为0并且您的条件是检查true值,但0不是,因此返回false。

This will fail if your matched index is 0 如果您匹配的索引为0则此操作将失败

m.indexOf('R:') === true 

Better you just check 更好的就是检查

m.indexOf('R:') !== -1

Ref: String.prototype.indexOf 参考: String.prototype.indexOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM