Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. var someArray = [{pattern:/runtime/, aProperty:"tag"},
  2. {pattern:/football/, aProperty:"blargh"},
  3. {pattern:/restaurant/, aProperty:"olives"}]
  4.  
  5. var runtimeString = "This is a runtime string provided by a user";
  6.  
  7. /** The string provided by a user might not actually have a match in
  8. someArray, in which case validItem would be undefined. Is there any danger
  9. or readability problem with comparing validItem to undefined to determine
  10. whether "find" returned a result? **/
  11.  
  12. var validItem = someArray.filter(obj => obj.pattern.test(runtimeString))[0];
  13. if (validItem !== undefined) {
  14. console.log("The matching item aProperty is: " + validItem.aProperty);
  15. } else {
  16. console.log("There was no matching item");
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement