Iterate through keys in an object literal and return the key name.

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

Returns the key name in mainObject if it's a key, and hasOwnProperty of key (to rule out the prototype object. Then it also checks if the key is listed in an array using a jquery function and also tests if it's null.

Copy this code and paste it in your HTML
  1. for (var key in mainObject){
  2. if(key && mainObject.hasOwnProperty(key) && ($.inArray(key, testAgainstThisArray) !== -1 ) && mainObject[key] !== null ) {
  3. console.log(key);
  4. }
  5. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.