0

I have an object that I am trying to parse through and delete properties that are empty. I'm having issues with javascript not equating the statement correctly.

var filters = { prop1 : "", prop2 : "set", prop3 : "" };
var f;
for( f in filters) {
    if(filters[f] === "") {
         delete filters[f];
    }
}

Using google's debug console, I know that filters[f] have given me "" for prop1 and the statement of filters[f] === "" equates to true, yet it doesn't enter inside the if statement block.

3
  • 6
    Your variable declaration says filter but you refer to it as filters later on. Commented Mar 25, 2015 at 21:49
  • Unable to reproduce, voting to close Commented Mar 25, 2015 at 21:53
  • I fixed the typo, it was my mistake when typing the question on my part. Commented Mar 25, 2015 at 21:54

1 Answer 1

1

If you fix the typo in your variable name, it seems to work like you expect

var filters = { prop1 : "", prop2 : "set", prop3 : "" };
//        ^ added
var f;
for( f in filters) {
    if(filters[f] === "") {
         delete filters[f];
    }
}
console.log(filters); // Object {prop2: "set"}
Sign up to request clarification or add additional context in comments.

Comments

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.