26

Suppose I have the next javascript object:

var errors = {
    error_1: "Error 1 description",
    error_2: "Error 2 description",
    error_3: "",
    error_4: "Error 4 description"
};

How can I determine if the property error_1 exists in the errors object and is not empty as well?

3
  • 2
    Are those error messages going to be only string always? Commented Feb 17, 2015 at 0:38
  • @Arvind In my case, they will be only strings. But if you ask to provide some solution for other data types, then that will be great, as me and the others will be able to use what fits for them. Commented Feb 17, 2015 at 0:51
  • What does "not empty" mean? Object properties are initialised to the supplied value, their default or undefined when created, so are never "empty". Commented Feb 17, 2015 at 1:02

4 Answers 4

33

if (errors.hasOwnProperty('error_1') && errors['error_1'] )

The method hasOwnProperty can be used to determine whether an object has the specified property as a direct property of that object.

The errors[key] where key is a string value checks if the value exists and is not null

to Check if its not empty where it is a string then typeof errors['error_1'] === 'string' && errors['error_1'].length where you are checking for the length of a string

Result:

if (errors.hasOwnProperty('error_1') && typeof errors['error_1'] === 'string' && errors['error_1'].length)

Now, if you are using a library like underscore you can use a bunch of utility classes like _.isEmpty _.has(obj,key) and _.isString()

Sign up to request clarification or add additional context in comments.

2 Comments

OP said "not empty" but this will also be true for 0 and null
thanks @Phil just added a String length check if he likes to specifically checks for non empty string
3

To precisely answer your question (exists and not empty), and assuming you're not referring to empty arrays, you could use

typeof errors.error_1 === 'string' && errors.error_1.length

2 Comments

dayum, so just plain old .length returns true if it's > 0, else false? You sure you don't need length > 0?
@RonRoyston yes. In a boolean expression, 0 is falsy
3

Here is a another good answer I found and wanted to share (after modification to fit my needs):

if ("property_name" in object_name && object_name.property_name !== undefined){
   // code..
}

So if I wanted to apply this on my example, it will look like:

if ("error_1" in errors && errors.error_1 !== undefined){
   // code..
}

1 Comment

the in operator looks down the property chain, so it might return unwanted results depending on the structure. hasOwnProperty checks the specified property as a direct property of that object
-2

In order to check whether the object is empty or not use this code.

if (Object.keys(object_name).length > 0) {

  // Your code

}

2 Comments

your code check if the object itself is empty, not the object property
not what the question is about

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.