Array/Object Detection


/ Published in: JavaScript
Save to your folder(s)

Detect if an object exists in an array or detect existance of a key in an object


Copy this code and paste it in your HTML
  1. /*
  2.  Author: Francois Lafortune (@quickredfox)
  3.  License: WTFPL
  4.  Description:
  5.  
  6.   Detect if an object exists in an array or detect existance of a key in an object
  7.  
  8.  Example:
  9.  (code)
  10.  
  11.   var myArray = [ 'red', 'green', 'blue' ];
  12.  
  13.   Detect.inArray( 'red' , myArray) // true
  14.   Detect.inArray( 'brown' , myArray) // false
  15.  
  16.   var myObject = {color: 'blue' };
  17.   Detect.inObject('color',myObject) // true
  18.   Detect.inObject('taste',myObject) // false
  19.  
  20.  (end)
  21. */
  22.  
  23. var Detect = (function() {
  24. var Detect = {
  25. inArray: function(key, array) {
  26. return (key in array);
  27. },
  28. inObject: function(key, object) {
  29. return (key.toString() in object)
  30. }
  31. }
  32. return Detect;
  33. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.