a guest Jun 25th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var someArray = [{pattern:/runtime/, aProperty:"tag"},
  2.                  {pattern:/football/, aProperty:"blargh"},
  3.                  {pattern:/restaurant/, aProperty:"olives"}]
  5. var runtimeString = "This is a runtime string provided by a user";
  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? **/
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand