Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[HELP] filterObjs question

1522196359
Missingquery
Pro
Sheet Author
API Scripter
Okay, so this is my code. As you can see, I'm trying to filter abilities that don't contain a specific string within them, then parse them as JSON and return the results: Skills = filterObjs(function(obj) {         if (obj.get('type') !== 'ability' || obj.action.indexOf('"triggertime": "turn"') == -1) return false;         let newobj = JSON.parse(obj.action);         newobj["source"] = obj.characterid;         return newobj;     }); When I run it, however, I get the error: TypeError: Cannot read property 'indexOf' of undefined Which makes sense, right? However, I was under the impression that due to short-circuited evaluation , the statement after the OR (obj.action.indexOf('"triggertime": "turn"') == -1) would never evaluate in the first place, so the error wouldn't be thrown. Since that's not the case, how do I correct my code so that it won't throw that error for non-ability objects?
1522198244
The Aaron
Pro
API Scripter
The obj passed to the filter function for filterObjs() is a Roll20 Option.  You need to use obj.get('action').indexOf().
1522198565

Edited 1522198585
The Aaron
Pro
API Scripter
That said, because of the indexing on things like type that Riley added, it's probably more efficient to do a findObjs() on type first, then filter and map: let Skills = findObjs({         type: 'ability'     })     .filter((obj)=> /"triggertime":\s*"turn"/.test(obj.get('action')))     .map((obj)=>({         ...JSON.parse(obj.get('action')),         source: obj.get('characterid')     }));