The functions used for string and array variables (bottom)

Source: Internet
Author: User

Three, array variable function

1) arrayobjecr.push (Newelement1,newelement2,... ...);

function: Adds one or more elements to the end of the specified array object. The method modifies the original array directly and returns the length of the new array.

This function with Arrayobject.unshift (newelement1,... ...) Correspond. The latter in turn adds elements to the head of the array.

Example:

<script>

var arr = ["I", "AM", "a", "little", "lovely", "Girl"];

var num = Arr.push ("cool");

document.write (arr); Output I,am,a,little,lovely,girl,cool

document.write (num); Output 7

</script>

2) Arrayobjecr.slice (start,end);

function: Returns the specified array from an existing array. Start specifies the beginning of the selection array, and when it is negative, it is calculated directly from the end, and end specifies the array subscript at the end, but returns does not include the element that corresponds to end. When end is not restricted, it is truncated by default from the start position to the end of the array.

The method does not change the original array, just returns a new array.

Example:

<script>

var arr = ["I", "a", "little", "lovely", "Girl"];

document.write (Arr.slice (2,4)); Output little,lovely

document.write (arr); Output I,am,a,little,lovely,girl original array does not change

</script>

3) Arrayobjecr.splice (index,howmany,item1 ...);

Action: Removes or adds an item to the array object. index specifies the position of the operation of the original array; howmany specifies that the number of items to delete is not deleted when it is 0, and item specifies that items added to the array, if not filled, indicate no additions.

This method changes the original array directly and needs to distinguish between its and slice () functions.

Example:

<script>

var arr = ["I", "AM", "a", "little", "lovely", "Girl"];

document.write (Arr.splice (4,0, "Lala")); Output I,am,a,little,lala,lovely,girl

document.write (Arr.splice (4,2, "smart")); Output I,am,a,little,smart,girl

document.write (arr); Output I,am,a,little,smart,girl The original array has changed

</script>

4) var newarr=arrayobjecr.filter (function () {...});

Action: Performs an operation in function on each element of an array object, returns true to preserve the element, and returns false to not be preserved.

The filter () method does not change the original array

Example:

<script>

var arr = [2,4,5,6,8,9,11];

var arr1 = Arr.filter (choose);

function choose (Element) {

if (element%2==0)

return true;

Else

return false;

}

document.write (ARR1); Output 2,4,6,8

document.write (arr); Output 2,4,5,6,8,9,11 original array unchanged

</script>

5) Arrayobjecr.sort (SortBy ());

Function: Sorts the array. SortBy is a function. parameter is not specified in the function, the default is to sort by character encoding (English alphabet), and if you do not want to specify the parameters and you want to sort the numbers, it is recommended that you convert the numbers to strings.

When comparing values, parameters A and B are called. The return value <0,a in front of B; The return value >0,a after B.

The method modifies the original array directly.

Example:

<script>

var arr = [55,66,22,88,33,11,99];

document.write (Arr.sort () (function () {

return a-B; Output 11,22,33,55,66,88,99 to the value from small to large, you can directly return the parameter A-b

}));

document.write (Arr.sort () (function () {

return b-a; Output 99,88,66,55,33,22,11 the values from the large to the small sort, can return the parameter directly b-a

}));

</script>

6) Arrayobjecr.reverse ();

Function: Reverses the order of the elements in the array.

This method will change the original array directly.

Example:

<script>

var str = "hello,world!";

var arr = str.split ("");

Arr.reverse ();

document.write (Arr.join ("")); Output! Dlrow,olleh

</script>

The functions used for string and array variables (bottom)

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.