Join () in JS, reverse () and split () function usage parsing

Source: Internet
Author: User
Tags what array

Join () in JS, reverse () and split () function usage parsing

<script>/**   1:arrayobject.reverse () *   Note: This method will change the original array without creating a new array. *2:arrayobject.join () *   Note: The Join () method is used to put all the elements in the array into a string. *      elements are delimited by the specified delimiter. *   Specify Delimiter method Join ("#"), where # can be any *3:stringobject.split (a, B) this is its syntax *      method: Used to split a string into an array of strings.    *      A is a must decision from a this division    *      B is not mandatory, optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.    *      Note: The returned array does not include a itself;    *      hints and notes:    *      Note: If an empty string ("") is used as a, then each character in the Stringobject is split.    *      Note: The action performed by String.Split () is the opposite of what Array.join does.   ***/var str = "a,b,c,d,e,f,g";//declares a string str  = Str.split (', '). reverse ();//split into an array object with the Split function, Then use the reverse function to arrange the array in reverse order 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 elements of an array.

Syntax: Arrayobject.sort (SortBy)

Note: a reference to an array. Note that the array is sorted on the original array, and no replicas are generated.

See this example to illustrate this point

<scripttype= "Text/javascript" >
var a=["DD", "CC", "ee", "AA"];
A.sort ();
var b=a;
document.write (A + "<br/>");
document.write (b);
</script>

Back to Aa,cc,dd,ee

aa,cc,dd,ee

Where SortBy is an optional value

* If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison.

* If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

    • If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
    • If a equals 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 (a);
alert (A.sort ());
alert (A.sort (MyFunction));
</script>

The ordering of parameters is determined by the size of the parameter

So you can understand.

To determine the value of any two members in the array, the return value determines the order in which the two members are sorted

This is because the original array is numeric, so here X, Y is a numeric type, X>Y returns a positive value, so it will be placed after Y, X=y, x<y return a negative value at this time x will be at Y

4.slice () method

You can return the selected element from an existing array.

Grammar
Arrayobject.slice (Start,end)

Start Required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
End is optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.

return value
Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.

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

Instead of modifying the array, the method returns a sub-array.

If you want to delete an element from an array, you should use Method Array.splice ().

Example:

Start and end are array subscript numbers, and the returned array does not include the end
Slice does not change the original array
<script type= "Text/javascript" >
var a=[99,8,2,6,4,4,6];
B=a.slice (1,4);
document.write (b + "<br/>");
document.write (a);
</script>
Return
8,2,6
99,8,2,6,4,4,6

5. Look at the splice () method

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

Grammar
Arrayobject.splice (index,howmany,element1,....., elementx)

Parameter description:

Index required. Specifies where to add/remove elements from.
This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number.
Howmany required. Specifies how many elements should be deleted. Must be a number, but it can be "0".
If this parameter is not specified, all elements from index start to the end of the original array are deleted.
Element1 is optional. Specifies the new element to be added to the array. Start the insertion from the subscript at index point.
Elementx is optional. You can add several elements to an array.

return value
If the element is removed from the Arrayobject, the 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 + "<br/>");//Returns an array of deleted elements
a=a.join ("|");
document.write (a);//splice () cannot expand its parameters, concat () can '
</script>

Return:


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

Description
The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.

Splice will directly modify the array

Example one:

<scripttype= "Text/javascript" >
var a=[99,8,2,6,4,4,6];
B=a.splice (1,4, "X", "Y");
document.write (b + "<br/>");
document.write (a);
</script>
Return
8,2,6,4
99,x,y,4,6

Example two:

<scripttype= "Text/javascript" >
var a = [n/a];
a = A.splice (0,2);
alert (a); //Output here a=[1,2]
a = A.splice;
alert (a); // output 2 here a=[2]
a = A.splice;
alert (a); // do not delete any arrays, output an empty array
</script>
Better explain that the splice will be modified directly to the array

Note: Unlike concat () , splice does not expand the parameters that he inserts. That is, if you insert an array, he is inserting the array itself, not the elements of the array.
When Concat () inserts an array, it expands the array, inserting the elements in the array, but in the inserted array
When there is an array, it will not unfold.

6.concat () method

The Concat () method is used to concatenate two or more arrays.

Note: This method does not alter an existing array , but simply returns a copy of the concatenated array.

Grammar
Arrayobject.concat (Arrayx,arrayx,......, Arrayx)

It returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array.

Example:

<scripttype= "Text/javascript" >
var a=[1,2,3];
var b=a.concat (1);
document.write (b + "<br/>");//Return 1,2,3,1
</script>

When the argument is an array, the elements in the array are added

When an array is contained in a parameter, the returned array cannot be disassembled

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 + "<br/>");//1,2,3
document.write (b + "<br/>");//3,4,5,6,7
document.write (c+ "<br/>");//1,2,3,3,4,5,6,7
document.write (d);//1#2#3#3#4#5,6,7
</script>


Code One:
<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+ "<br/>");
document.write (Js.join ("|"))
</script>
return:
1|2|3|1,2,3| | | X|y|z
Why do you return some empty values here??
I understand:
because a in varb=[a,b,c] has already been assigned a value, it returns a number of one and the BC has not been assigned so return a blank ...

The following does not have a variable a assignment, which returns the undefined
Code two:
<scripttype= "Text/javascript" >
var A;
document.write (a);
</script>

but if the declaration of a is an array, then the blank is returned ·
<scripttype= "Text/javascript" >
var a=new Array ();
document.write (a);
</script>

7.

The push () method adds one or more elements to the end of the array and returns the new length . Note is to return a length
Grammar
Arrayobject.push (newelement1,newelement2,...., newelementx) parameter description
Newelement1 required. The first element to add to the array.
Newelement2 is optional. The second element to add 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 adds its parameter order to the tail of the arrayobject. It modifies the arrayobject directly, rather than creating a new array. the Push () method and the Pop () method use the advanced post-stack functionality provided by the array.

Hints and Notes
Note: This method will change the length of the array.

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

Examples (similar to unshift and push)
<script type= "Text/javascript" >
var array1 = new Array ("1", "2", "3", "4");
Array1.push ("5" );
document.write (array1+ "<br/>" );
document.write (array1); //Return value indicates that the method directly modifies the array
</script>

Another pair:

The shift () method removes the first element from the array and returns the value of the first element.

<scripttype= "Text/javascript" >
var array1 = NewArray ("1", "2", "3", "4");
Array1.push ("5");
document.write ( array1+ "<br/>");// Output 1, 2, 3, 4,5
Array2=array1.shift ( )
document.write (array2+ "<br/>"); Output 1
document.write ( array1);// Output 1, 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+ "<br/>");// Output 1, 2, 3, 4,5
Array1.pop ()
document.write (array1+ "<br/>");// Output 1, 2, 3, 4
document.write (Array1.pop ());// Output 4
</script>


Join () in JS, reverse () and split () function usage parsing

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.