Need help with JS includes method

I’m looping over some records and want to increment ‘i’ ONLY if both conditions below are met:

let i = 0;
for (let record of taskRecords.records) {
    if(id.includes(record.getCellValue('ID Lookup')) && record.getCellValue('Stage').includes(stage)) {
        i++;
    }
}

console.log(i);

My problem is the second condition. The cell value of the field ‘Stage’ is “This is just some dummy text | this is a string” and it’s searching for “this is a string” (this is called ‘stage’ and is a string from a previous step in an automation).

i remains 0 in this case. Can anyone explain why includes doesn’t work on the second condition? Thanks!

What type of field is your ‘Stage’ field?

Depending on the field type it may need to be coerced into a string. Maybe try using the method getCellValueAsString instead of getCellValue?

2 Likes

Hi,

just guessing - string method ‘includes’ is case-sensitive, if your “this is a string” in cell starting from capital, try to search for “his a string”

1 Like

Here’s a generic troubleshooting tip.

Assign the cell values to variables. Then console.log all the variables before your `if`` statement. One or more of the values may not be what you expect.

3 Likes

On top of that, if you’re polling non-array stuff with Array.prototype.includes, you’re going to have a bad time, that syntax only works due to destructuring coercion shenanigans which really aren’t the type of behavior you want your code to depend on. Look into substring/substr methods if the cell value is a string.

Both strings and arrays have an includes() method. So, if you don’t know what data type you have (array versus string), you might get expected results for the wrong reasons.

This was it. I was running .includes on an object and not a string. So that worked, thank you!

1 Like

As @kuovonne mentioned, .includes exists for strings as well. For my understanding (JS noob here): where do you see the issue with that? Thanks

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.