Posted By

quickredfox on 08/20/09


Tagged

object array detection inarray inObject


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

sheedy


Array/Object Detection


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.