简体   繁体   中英

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. Shouldn't it be true?

Why is the first element in the replies array 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.

This will fail if your matched index is 0

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

Better you just check

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

Ref: String.prototype.indexOf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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