1

Below is the object returned from backend:

[Object]0: Object Option_Name_0: null Option_Name_1: "me" Option_Name_2: "you" Option_Name_3: "get" Option_Name_4: "no"__proto__: Objectlength: 1__proto__: Array[0]

I am just trying to populate the values into dropdown menu, by removing "null" value.

$.each(e, function(i) {
    $.each(e[i], function(key, val) {
        if (val != 'null') {
            $(".flash_bet_win").append("<option value=" + val + ">" + val + "</option>");

        }

    });
});

But still I see "null" value in dropdown menu. How to fix this?

2
  • Are you sure it is a string and not null Commented Jul 20, 2015 at 16:09
  • 1
    Remove the quotes from around null Commented Jul 20, 2015 at 16:09

2 Answers 2

2

Check for null not "null",

if(val !== null)
            {
                 $(".flash_bet_win").append("<option value="+val+">"+val+"</option>");

            }

go for a deep comparison (!==) not (!=) as undefined == null would result in truthy value whereas undefined === null would be false

In case you want to check if the value is not null, not undefined or not empty use,

if(val){
    ...... //your code
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your if statement, you're checking that that it's not equal to string 'null', when you want to check if it's equal to the value null

if(val != null)

Or, even shorter:

if(val)

Does this do the trick?

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.