Hello reader's in this tutorial we will validating the array contents. We want that array meets the certain criteria. I am using the array object's method to check every given element.
For example, the following code checks an alphanumeric character under every element in the array:
Syntax:-
var elemSet = new Array("**",123,"aaa","abc","-",46,"AAA");
// testing function
function textValue (element,index,array) {
var textExp = /^[a-zA-Z]+$/;
return textExp.test(element);
}
// run test
alert(elemSet.every(textValue)); // false
Or to check that at least some of the elements pass the comparison by using the Array object’s some method:
Syntax:-
var elemSet = new Array("**",123,"aaa","abc","-",46,"AAA");
// testing function
function textValue (element,index,array) {
var textExp = /^[a-zA-Z]+$/;
return textExp.test(element);
}
// run test
alert(elemSet.some(textValue)); // true
The each and some Array object strategies won't conflict with all cluster components, to satisfy the usefulness procedure the same number of as exhibit components as vital. callback capacity can be utilized for both the each and the some Array object techniques will be show in an answer. At the point when utilizing the strategy "each" the capacity when will give back a false, the handling will be done and the outcome will returned is false.
Until the callback capacity returns genuine, the some strategy will keep on testing against each cluster component. No different components are approved around then, and after that technique returns genuine. The some technique will gives back the quality false, when the callback capacity tests against all components and does not give back the worth valid anytime. It relies on upon you which strategy is appropriate for you.
Utilize "each" technique when every one of the components meet certain criteria generally utilize "some" strategy. The three parameter is taken by the callback capacity are the component, index for element and the array.
NOTE:- The some or every method are not supported by IE8, but they are supported by the other target browsers.
0 Comment(s)