Ajax Getting Started Guide (vi)

Source: Internet
Author: User
Tags array object continue include regular expression return
An introductory guide to Ajax learning is believed to be helpful to Ajax beginners.

Prototype framework
Enumeration object (enumerable object)
1.enumerable.each (iterator)
The parameter of each method is a JavaScript function that can accept two parameters, which are the current traversal of the collection element and the ordinal of the current traversal element in the collection
Example:
var arr = [1,2,3,4];
Arr.each (
function (Item,index) {
if (index = = 2)
Alert ("arr[" +index+ "]=" +item)///When the serial number is 2, the output "arr[2]=3"
}
);

2. $break and $continue objects
To implement certain process control when traversing the collection elements
Example:
var arr = [1,2,3,4,8,5,4,3];
The output is: 1,2,3,4,5,4,3
Arr.each (
function (Item,index) {
if (item < 6) {
alert (item);
} else{
A number greater than or equal to 6 is skipped and the next element continues to be processed
$continue; If you follow the example below with throw $continue will end the traversal
}
}
);

var arr = [1,2,3,4,8,5,4,3];
The output is: 1,2,3,4,5,4,3
Arr.each (
function (Item,index) {
if (item < 6) {
alert (item);
} else{
A number greater than or equal to 6 is skipped and the next element continues to be processed
Throw $break;
}
}
);

3.enumerable.all (iterator)
Used to determine whether all elements within a collection meet a condition, and if the condition returns True, the All method returns True, otherwise false
Example:
var arr = [1,2,3,4,8,5,-4,3];
var Boolean = Arr.all (
function (Item,index) {
Return (item>0)
}
);
Alert (Boolean); Output false;

4.enumerable.collect (iterator)
Iterates through all the elements in the collection, and returns the result of an array object in the order of the collection.
Example:
var arr = [1,2,3,4,8,5,4,3];
var newArr = Arr.collect (
function (Item,index) {
Return (item * Item)
}
);
alert (NEWARR);//Output "1,4,9,16,64,25,16,9"

5.enumerable.find (iterator)
Returns the element value or Boolean value that satisfies the condition
Example:
var arr = [1,2,3,4,8,5,4,3];
var ele = Arr.find (
function (Item,index) {
if (item > 3)
Return (item)
}
);
alert (ele);//Output value: 4
The bold part can be replaced by: Return (item > 3) returns the Boolean value used to determine whether the collection has the element that satisfies the condition

6.enumerable.findall (iterator)
All elements that meet the criteria are returned with an array object
var arr = [1,2,3,4,8,5,4,3];
var ele = Arr.findall (
function (Item,index) {
if (item > 3)
Return (item)
}
);
alert (ele);//Output "4,8,5,4"

7.enumerable.grep (Pattern,iterator)
The pattern parameter is a regular expression,
Example:
var arr = ["12345", "ABC2", "CDE", "FGH", "132BA"];
var NewArray = Arr.grep (
/2/,
function (Item,index) {
alert (item);//Output "12345", "ABC2", "132BA"
}
);
8.enumerable.include (obj)
To determine if the Obj object is a member of a collection
Example:
var arr = [1,2,3,4,5];
var ele = arr.include (2);
alert (ele);//Output True

9.enumerable.max (iterator)
Returns the largest element in a collection
Example:
var arr = [1,2,3,4,5];
var ele = Arr.max ();
alert (ele); Output 5

10.enumerable.min (iterator)
Returns the smallest element in a collection

11.enumerable.sortby (iterator)
To sort
Example:
var arr = [1,5,3,2,4];
var arr = Arr.sortby (
function (Item,index) {
return item;
}
);
Alert (arr);//Output "1,2,3,4,5"



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.