Analysis of join (), reverse () and split () functions in js

Source: Internet
Author: User

Analysis of join (), reverse () and split () functions in js

Analysis of join (), reverse () and split () functions in js

<Script>/** 1: arrayObject. reverse () * Note: This method changes the original array instead of creating a new array. * 2: arrayObject. join () * Note: The join () method is used to put all elements in the array into a string. * Elements are separated by the specified delimiter. * Specify the separator method join ("#"); where # can be any * 3: stringObject. split (a, B) is its syntax * method: used to split a string into a string array. * a is required to determine the Division from a * B is not required and optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length. * Note: The returned array does not contain a itself. * Note and Note: * Note: if an empty string ("") is used as a, each character in the stringObject is separated. * Note: The operations executed by String. split () are the opposite of those executed by Array. join. * **/Var str = "a, B, c, d, e, f, g"; // declare a string str = str. split (','). reverse (); // use the split function to split the object into an array, and then use the reverse function to sort the array in reverse order by alert (str. length + ":" + typeof (str); alert (typeof (str. join) + ":" + str. join ('#') + ":" + str. join ('#'). length); var s = str. reverse (). join ("#"); alert (typeof (s) + ":" + s); </script>
Knowledge Development:

3.

Sort () method

The sort () method is used to sort the elements of an array.

Syntax: arrayObject. sort (sortby)

Note: References to arrays. Note that arrays are sorted on the original array without generating copies.

See this example to illustrate this point.

<Scripttype = "text/javascript">
Var a = ["dd", "cc", "ee", "aa"];
A. sort ();
Var B =;
Document. write (a +"
");
Document. write (B );
</Script>

Return aa, cc, dd, ee

Aa, cc, dd, ee

Sortby is an optional value.

* If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, first convert the elements of the array into strings (if necessary) for comparison.

* If you want to sort the values according to other criteria, you need to provide a comparison function that compares two values and returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, a and B. The returned values are as follows:

If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned. If a is equal to B, 0 is returned. If a is greater than B, a value greater than 0 is returned.

Example:

<Scripttype = "text/javascript">
Function myfunction (x, y ){
Return x-y;
}
Var a = new Array (3 );
A [0] = "1111 ";
A [1] = "22 ";
A [2] = "333 ";
Alert ();
Alert (a. sort ());
Alert (a. sort (myfunction ));
</Script>

Parameter sorting is determined by the parameter size.

This is understandable.

The value of any two members in the array is determined, and the return value determines the regular order of the two members.

Here, because the original number group is numeric, the x and y here are numeric, And the return value is a positive value when x> y, So x will be placed behind y, x = y will not change, x

4. slice () method

You can return the selected elements from an existing array.

Syntax
ArrayObject. slice (start, end)

Start is required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array. That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on.
End is optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array.

Return Value
Returns a new array containing elements in the arrayObject from start to end (excluding this element.

Note that the end element is not included !!!!!!!!!!!

This method does not modify the array, but returns a child array.

If you want to delete an element from an Array, use the method Array. splice ().

Example:

Both start and end are array subscript numbers, and the returned array does not contain end
Slice will not change the original array
<Script type = "text/javascript">
Var a = [, 8, 6];
B = a. slice (1, 4 );
Document. write (B +"
");
Document. write ();
</Script>
Return
8, 2, 6
, 6

5. Check the splice () method.

The element used to insert, delete, or replace an array.

Syntax
ArrayObject. splice (index, howmany, element1,..., elementX)

Parameter description:

Index is required. Specifies where to add/delete elements.
This parameter is the subscript of the array element that begins to insert or delete. It must be a number.
Howmany is required. Specifies how many elements should be deleted. It must be a number, but it can be "0 ".
If this parameter is not specified, all elements starting from index to the end of the original array are deleted.
Optional. Specifies the new element to be added to the array. Insert from the subscript indicated by index.
ElementX is optional. You can add several elements to the array.

Return Value
If an element is deleted from an arrayObject, an array containing the deleted element is returned.

Example:
<Script type = "text/javascript">
Var a = [11, 22, 33];
B = a. splice (0, 1, "x", "y", [6, 7, 8]);
Document. write (B +"
"); // Returns the array of deleted elements
A = a. join ("| ");
Document. write (a); // splice () cannot expand its parameters. concat () can be''
</Script>

Return Value:


X | y | 6, 7, 8 | 22 | 33

Description
The splice () method deletes zero or multiple elements starting from the index and replaces the deleted elements with one or more values declared in the parameter list.

Splice directly modifies the array.

Example 1:

<Scripttype = "text/javascript">
Var a = [, 8, 6];
B = a. splice (1, 4, "x", "y ");
Document. write (B +"
");
Document. write ();
</Script>
Return
8, 2, 6, 4
99, x, y, 4, 6

Example 2:

<Scripttype = "text/javascript">
Var= [1, 2, 3];
A = a. splice (0, 2 );
Alert ();// Output 1, 2 Here a = [1, 2]
A = a. splice (1, 2 );
Alert ();//Output 2 Here a = [2]
A = a. splice (1, 2 );
Alert ();//No array is deleted, and an empty array is output.
</Script>
Better Description: splice directly modifies the array.

Note: Unlike concat (), splice does not expand the inserted parameters. That is, if an array is inserted, It is inserted into the array itself, and it is not an array element.
If concat () is inserted into an array, it will expand the array and insert the elements in the array. However, when the inserted array is
When there is an array, it will not be expanded.

6. concat () method

The concat () method is used to connect two or more arrays.

Note: This method does not change the existing array, but only returns a copy of the connected array.

Syntax
ArrayObject. concat (arrayX, arrayX,..., arrayX)

It returns a new array. This array is generated by adding all arrayX parameters to arrayObject. If the concat () operation parameter is an array, the elements in the array are added instead of an array.

Example:

<Scripttype = "text/javascript">
Var a = [1, 2, 3];
Var B = a. concat (1 );
Document. write (B +"
"); // Return 1, 2, 3, 1
</Script>

When the parameter is an array, the elements in the array are added.

When an array is included in a parameter, the returned array cannot be split.

Example

<Scripttype = "text/javascript">
Var a = [1, 2, 3];
Var B = [3, 4, [5, 6, 7];
Var c = a. concat (B );
D = c. join ("#")
Document. write (a +"
"); // 1, 2, 3
Document. write (B +"
"); // 3, 4, 5, 6, 7
Document. write (c +"
"); // 1, 2, 3, 4, 5, 6, 7
Document. write (d); // 1 #2 #3 #3 #4 #5, 6, 7
</Script>


Code 1:
<Scripttype = "text/javascript">
Var a = [1, 2, 3];
Var B = [a, B, c];
Var c = ["x", "y", "z"];
Var js = a. concat (B, c );
Document. write (js +"
");
Document. write (js. join ("| "))
</Script>
Return Value:
1 | 2 | 3 | 1, 2 | x | y | z
Why are some null values returned here ??
I understand:
Because a in varb = [a, B, c] has been assigned a value, so 1, 2, 3 are returned.Bc has not been assigned a value, so a blank space is returned ···

No value is assigned to variable a, and undefined is returned.
Code 2:
<Scripttype = "text/javascript">
Var;
Document. write ();
</Script>

However, if declared a is an array, the white space is returned ··
<Scripttype = "text/javascript">
Var a = new Array ();
Document. write ();
</Script>

7.

The push () method can add one or more elements to the end of an array and return a new length. // Note that a length is returned.
Syntax
ArrayObject. push (newelement1, newelement2,..., newelementX) parameter description
Newelement1 is required. The first element to be added to the array.
Newelement2 is optional. The second element to be added to the array.
NewelementX is optional. You can add multiple elements.

Return Value
Adds the specified value to the new length after the array.

Description
The push () method can add its Parameter order to the end of arrayObject. It directly modifies the arrayObject instead of creating a new array. The push () and pop () methods use the advanced post-stack functions provided by arrays.

Tips and comments
Note: This method will change the length of the array.

Tip: to add one or more elements at the beginning of an array, use the unshift () method.

Example (unshift and push are similar)
<Script type = "text/javascript">
Var array1 = new Array ("1", "2", "3", "4 ");
Array1.push ("5 ");
Document. write (array1 +"
");
Document. write (array1 ); // The same as the return value indicates that the method directly modifies the array.
</Script>

Another pair:

Shift () is used to delete the first element of the array from it and return the value of the first element.

<Scripttype = "text/javascript">
Var array1 = newArray ("1", "2", "3", "4 ");
Array1.push ("5 ");
Document. write (array1 +"
"); // Output1, 2, 3, 4, 5
Array2 = array1.shift ()
Document. write (array2 +"
"); // Output 1
Document. write (array1); // output1, 2, 3, 4
</Script>
Pop () deletes the last element and returns the last element.
<Script type = "text/javascript">
Var array1 = new Array ("1", "2", "3", "4 ");

Array1.push ("5 ");
Document. write (array1 +"
"); // Output1, 2, 3, 4, 5
Array1.pop ()
Document. write (array1 +"
"); // Output1, 2, 3, 4
Document. write (array1.pop (); // output 4
</Script>


Related Article

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.