JAVA SCRIPT - Validating Array Contents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Validating Array Contents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, December 23, 2018

JAVA SCRIPT - Validating Array Contents

Validating Array Contents




Problem

You want to ensure that array contents meet certain criteria.

Solution

Use the Array every() method to check that every element passes a given criterion. For instance, the following code checks to ensure that every element in the array consists of alphabetical characters: 

// testing function
function testValue (element,index,array) {
 var textExp = /^[a-zA-Z]+$/;
 return textExp.test(element);
}
var elemSet = ["**",123,"aaa","abc","-",46,"AAA"];
// run test
var result = elemSet.every(testValue);
console.log(result); // false
var elemSet2 = ["elephant","lion","cat","dog"];
result = elemSet2.every(testValue);
console.log(result); // true
Or use the Array some() method to ensure that one or more of the elements pass the criteria. As an example, the following code checks to ensure that at least some of the array elements are alphabetical strings:
var elemSet = new Array("**",123,"aaa","abc","-",46,"AAA");
// testing function
function testValue (element) {
 var textExp = /^[a-zA-Z]+$/;
 return textExp.test(element);
}
// run test
var result = elemSet.some(testValue);
console.log(result); // true

EXPLAIN

Unlike the Array methods I covered earlier in the chapter, every() and some() functions do not work against all array elements: they only process as many array elements as necessary to fulfill their functionality.

The solution demonstrates that the same callback function can be used for both the every() and some() methods. The difference is that when using every(), as soon as the function returns a false value, the processing is finished, and the method returns false. The some() method continues to test against every array element until the call‐ back function returns true. At that time, no other elements are validated, and the method returns true. However, if the callback function tests against all elements, and doesn’t return true at any point, some() returns false.

Which method to use depends on your needs. If all array elements must meet certain criteria, then use every(); otherwise, use some(). The callback function takes three parameters: the element, and an optional element index and array.

Using Anonymous Functions in Array Methods 


I used an anonymous function, but in this solution, I use a named function. When to use which depends on the context and your own preferences. The advantage to a named function is it shows up in a stack trace when you’re debugging your code, while an anonymous function doesn’t. This isn’t an issue when you’re dealing with simple, targeted functionality, as demonstrated in all of these solutions.

However, it can very much be an issue in functionality that’s deeply nested, or is asynchronous. Another advantage to the named function is you can use it in multiple places. Again, though, it doesn’t make sense to reuse a function targeted to a specific Array method callback for any reason other than that specific callback. And the disadvantage is that you are cluttering up the global space when you use a named function.

 A final possible advantage is that named functions perform better in all browsers than anonymous functions in the context of an Array method callback. Now, this might be the best reason for using a named function.

No comments:

Post a Comment

Post Top Ad