2

I'm looking to loop through an object and delete values that are deemed to be "falsey", ie: false, null, 0, Nan, etc.

So far, my code looks like this:

function truthyObjLoop(user) {
var falseAnswer = undefined;
for (var key in user) {
  if (user[key] === false) {
    falseAnswer += user[key];
    delete falseAnswer;
  }
}
return user;

}

I know it's bugged, but I'm having a difficult time locating and fixing the issue. I'm very new to Javascript and any help is appreciated.

Thanks.

2
  • shouldn't it be delete user[key] ?? Commented Aug 21, 2015 at 18:49
  • user[key] === false will only be true if user[key] is indeed false (not any falsy value). Commented Aug 21, 2015 at 18:54

2 Answers 2

2

You were rather close in your first attempt. However, comparing === false will not always catch "falsey" values. Simply using !value on and checking for true will look for falsey values because it implicitly casts the value to boolean. At that point all that is left is to iterate and remove.

function truthyObjLoop(user){
    for(var key in user){
        if(user.hasOwnProperty(key) && !user[key]) delete user[key];
    }
    return user;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think there is any problem deleting the property one just visited.
@FelixKling - That wont cause side affects? Is that because they are considered to be out of order anyway? I was unsure what the internal enumerator's behavior for for( var key in obj) would be in that case.
It seems to be fine: "Properties of the target object may be deleted during enumeration. A property that is deleted before it is processed by the iterator’s next method is ignored. If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration. A property name will be returned by the iterator’s next method at most once in any enumeration."
I guess it does something similar than you do explicitly.
@FelixKling - Okay, thanks for the clarification. I will edit that part out of my answer then and put the shorter version in.
0

If you only want to catch values which are equivilant to false, null, undefined, or 0... you should use @travis j's answer. however if you want to also match NaN values, i'd suggest a minor adjustment

function truthyObjLoop(user){
for(var key in user){
    if(user.hasOwnProperty(key) && (!user[key] || isNaN(user[key]) )) delete user[key];
}
return user;
}

if you can extend it even further to include empty arrays and empty objects

function truthyObjLoop(user){
for(var key in user){
if( user.hasOwnProperty(key) && (!user[key] ||
     Number.isNaN(user[key]) || 
     (user[key] instanceof Array && !user[key].length) || 
     (user[key] instanceof Array ==false && user[key] instanceof String == false && !Number.isInteger(user[key]) && !Object.keys(user[key]).length) )) 
    delete user[key];
}
return user;
}
}

1 Comment

You don't need the extra isNan() test. NaN values are falsy. Try this in the console: console.log( (0/0), !!(0/0), !!false ); It will log NaN false false. (The !! converts falsy values to false and truthy values to true.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.