1

What would be a better way to try checking that response is an array with length?

try{
    response.errors.length > 0;
    deferred.reject(response)
}
catch(e){
    deferred.resolve(response);
}
3
  • 1
    The length check will not throw an exception. What might is response or response.errors evaluating to undefined. Commented Oct 2, 2013 at 18:41
  • possible duplicate of Checking if an associative array key exists in Javascript Commented Oct 2, 2013 at 18:45
  • To be clear, I want this expression to evaluate in the context of try catch so that I can capture the exception. As per jshint: "Expected an assignment or function call and instead saw an expression." All the answers imply that I should not even be using try, which is fine. I'm just wondering how I would satisfy the jshint error. Commented Oct 2, 2013 at 19:45

5 Answers 5

2

This will make sure that errors is an array and it has items in it.

if (Array.isArray(response.errors) && response.errors.length) {
    deferred.reject(response);
} else {
    deferred.resolve(response);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I dare say never check for if something is-an array; that's what duck typing is for ;-) The exception would be if I need to perform some kind of "overloading" based on a parameter - in which case the explicit check is more prudent.
@user2246674 Agreed. But this is what the OP want I believe. checking that response is an array with length
I suppose that is a reading - although not mine :) This answer is perfectly correct in the provided context.
1

One way is:

if (response.errors.length) {
    deferred.reject(response);
} else {
    deferred.resolve(response);
}

This is using the fact that 0 is considered falsey in JavaScript, and any other number is considered truthy.

If you're worried about the type of response.errors, you can add response.errors instanceof Array to the if condition:

if (response.errors instanceof Array && response.errors.length) {

6 Comments

How do we make sure that it is an Array? Even functions can have length property.
@thefourtheye Good point, this is arguably in scope for this question, edited.
@thefourtheye We don't care if it's an array. Duck-typing.
@user2246674 Why don't we care? The question clearly asks specifically about arrays, there may be the need for it. No reason not to include that bit of knowledge.
@ajp15243 Because it's an orthogonal topic - the meat of the question is how to handle where there may be an optional array, not that there is an arbitrary value which may be an array.
|
0

What about

if (response.errors && response.errors.length > 0)
   deferred.reject(response);
else
    deferred.resolve(response);

No need to catch exceptions from accessing non-existent properties, just test for their existence before…

Comments

0

If you want to be explicit about checking that it is an array.

if (response.errors && (response.errors instanceof Array) && response.errors.length > 0)
   deferred.reject(response);
else
   deferred.resolve(response);

(Tested in FF and Chrome).

Comments

0

The length check (length > x) will not throw an exception. What might throw an exception is if response or errors evaluates to undefined (e.g. doesn't exist).

if (response && response.errors && response.errors.length) {
  // Have errors
  // The check for response might not be needed, but one should most
  // definitely ensure that `response.errors` evaluates to an object (Array?)
  // before trying to access a property upon it.
}

Note that this uses && short-circuiting behavior.

Alternatively, sometimes it's nice to normalize data (assumes response is always an object). In this case we make sure that we have an "empty array" as needed such that we have some array on which to check the length.

response.errors = response.errors || []
if (response.errors.length) {
   // Have errors
}

In the above examples I also utilize that a non-0 number in JavaScript is a truth-y value, which allows if (length > 0) .. to be written as if (length) .. and it will work reliably assuming that length is always a number, when present.

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.