0

I want to use a for-in loop to check if items in an object are undefined. However the loop will only run if there are already values stored in the object.

Here's an example of how I'm putting values into the object:

<div>
  <input type="radio" name="" id="" ng-model="user.fac_staff" value="">
  <input type="radio" name="" id="" ng-model="user.fac_staff" value="">
  <input type="radio" name="" id="" ng-model="user.fac_staff" value="">
</div>

<div>
  <select class="" id="" name="" ng-options="ng repeat stuff" data-ng-model="user.college">
    <option value="">Stuff</option>
  </select>
</div>

<div>
  <input type="radio" name="" id="" ng-model="user.source" value="">
  <input type="radio" name="" id="" ng-model="user.source" value="">
  <input type="radio" name="" id="" ng-model="user.source" value="">
</div>

<input type="text" class="" name="" id="" data-ng-model="user.name_awarding_agency">

<button type="submit" ng-click="ajs_function(user)">Click</button>

Here's an example of what I'm trying to do in the controller:

$scope.ajs_function = function(userObj) {

    for(var key in userObj) {
        if(userObj[key] == "undefined")
            //do stuff
    }
}

The loop won't run at all. I've tried to display the key values to the console, and when that didn't work I tried to display a counter value to the console just to see if it was doing anything at all. I should also mention that I have a similar for-in loop in a function going through the same object, however that loop only runs when there is already data.

Daniel Rumfelt
  • 85
  • 1
  • 11

2 Answers2

0

Try this instead:

for(var key in userObj) {
    if(typeof userObj[key] === "undefined")
        //do stuff
}

You were testing if the value of the object property was equal to the string literal "undefined". You should be testing if the type of the object property is undefined.

Detecting an undefined object property

Community
  • 1
  • 1
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • I actually tried that before asking and I didn't get a different outcome. Is a for-in the right thing to do this with? – Daniel Rumfelt Sep 23 '15 at 21:25
  • What outcome are you getting? Certainly using `typeof` is the right thing to do, but you might have other things happening in there causing problems, too. – Jonathan M Sep 23 '15 at 21:29
  • I'm getting absolutely nothing for an outcome. The loop isn't even iterating through the object as far as I can tell. Also, I'm sure typeof is the correct way to do this as I've seen it used in many other sources, though I'm not sure why it is not working for me. However, is there something else other than a for-in loop that could do this better, or simply an alternative way, maybe? – Daniel Rumfelt Sep 24 '15 at 20:58
0

(Not enough reputation to comment, so please don't -1 me if this isn't the solution...)

Try console logging the parameter at the start of your function to see what those values look like once passed.

$scope.ajs_function = function(userObj) {
    console.log('userObj: ', userObj);
    for(var key in userObj) {
        if(userObj[key] == "undefined")
            //do stuff
    }
}

I'm assuming it's not doing what you expect, because you're setting value="", which may be setting those values to empty strings, which is not undefined, depends on how you're initializing those variables in your .js file.

My guess is your object might log to the console looking like this:

user: {
  college: [""]
  fac_staff: ""
  name_awarding_agency: ""
  source: ""
}

A good rule to follow when using for...in loops in angularjs is to include a hasOwnProperty check:

$scope.ajs_function = function(userObj) {
   for (var key in userObj) {
      if (!userObj.hasOwnProperty(key)) {
         continue;
      }
   // continue code here...
}

   
Derek K
  • 61
  • 7