1

I am iterating arrays within arrays what I have. Some inner arrays does not have certain attribute called "validations" and some does. Whenever my loop hits an array with no certain attribute, it throws an error saying "no validations" and stop looping.

I'm calling this function:

$scope.groups[a].sections[0].fields[1]["validations"]

I wonder if there's a way to skip current array if the array does not have validations.

Thanks

1

1 Answer 1

1

When a property is not defined, it evaluates to undefined when addressed. As a boolean, undefined evaluates to false.

if ($scope.groups[a].sections[0].fields[1]["validations"]) {
  ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Trying to use your block of code results in a ReferenceError: $scope is not defined
yeah so how do I ignore the error and keep moving on in the loop?
@Kahsn add the code with "the loop" to your question - so that I can understand what loop you are talking about
@Igor if ($scope.groups[a].sections[0].fields[1].['valadations']) { console.log('All these fields exist!') } console.log('No error will stop me!') will log no text.
@Igor It looks like we're thinking about this differently. I thought the question was about any of the attributes on the way down being undefined. In F#, you can do something like foo?baz?bar and if of those is null on the way down, it returns null. Now I see you're just dealing with valuations not existing.

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.