0

I have a scenario where i need to check whether a variable is not null using java script

Now these variables can be generated automatically and their naming convention is going to be like below

**Attribute.1.Name='aaa'
Attribute.2.Name='aaa'
Attribute.3.Name=''**

and so on.

how do i validate something like this where i do not now the exact variable name. All i know is the pattern of the variable.

Code example

FunctionName({'Attribute.1.Name':'test','Attribute.2.Name':'test2'});

Thanks

3
  • How are those variables generated? In which scope are they stored? Commented Mar 16, 2012 at 8:58
  • so the user will write them while calling one of my api's, so basically this way user can pass multiple values for one property Commented Mar 16, 2012 at 8:59
  • Code example please. How is the user calling your API? Commented Mar 16, 2012 at 9:00

1 Answer 1

1
var FunctionName = function(parameters) {
    if (parameters['Attribute.1.Name'] == null) {
        ...
    }
}

and if you wanted to loop through all properties of the object:

var FunctionName = function(parameters) {
    for (var name in parameters) {
        if (parameters.hasOwnProperty(name)) {
            if (parameters[name] == null) {
                ...
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Darin-- these parameters are dynamic and user can enter any number of them. so hard coding won't work
@Amit, then you could loop through them as shown in my second example.

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.