Some methods of arrays and strings in JS

Source: Internet
Author: User
Tags what array

Some methods of arrays:

1.join () and Split () methods
<script type= "Text/javascript" >
var x;
var a=new Array ();
A[0]= "XHTML";
A[1]= "CSS";
A[2]= "JavaScript";
Alert (A.join ());
Alert (typeof (A.join));
Alert (typeof (a));
</script>
The join () method is used to put all the elements in an array into a string.

The elements are delimited by the specified delimiter.
Specifies the delimiter method join ("#"), where # can be any

The opposite is the split () method: Used to split a string into an array of strings.
Stringobject.split (A, B) this is its syntax
A is necessary to decide on a this division from a
B is not required, 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 that 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 will be split.

Note: The action performed by String.Split () is the opposite of what Array.join does.

See Example
<script type= "Text/javascript" >
var str= "How is it?";
document.write (Str.split ("") + "<br/>");
document.write (Str.split ("") + "<br/>");
document.write (Str.split ("", 3) + "<br/>");
</script>
Return
H,o,w, A,r,e,, Y,o,u,?
How,are,you?
H,o,w

Two ways to divide sentences into words
var words = Sentence.split (") or use regular expressions as a:

var words = Sentence.split (//s+/)

2.
Reverse () method

The reverse () method reverses the order of the elements in the array.

Grammar:

Arrayobject.reverse ()

Note: This method changes the original array without creating a new array.


<script type= "Text/javascript" >
var a=["xhtml", "CSS", "JavaScript"];

var b=a.reverse ();
document.write (a);
document.write (b);
</script>

var b=a.reverse ();

A.reverse ();

var b=a;

The reverse () method changes the original array, so the return value A of the example above is also changed!

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

<script type= "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:

<script type= "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:

<script type= "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:

<script type= "Text/javascript" >
var a = [n/a];
A = A.splice (0,2);
alert (a); Output 1, 2 here a=[1,2]
A = A.splice;
alert (a); Output 2 here A=[2]
A = A.splice;
alert (a); No array deleted, output 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:

<script type= "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

<script type= "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:  
<script type= "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 are some empty values returned here??  
I understand:  
because the A in Var b=[a,b,c] has been assigned a value so return 1,2,3  and B C has not been assigned so return a blank ... &NBSP

This does not have a variable a assigned value, returns the UNDEFINED&NBSP;
Code two:  
<script type= "Text/javascript" >&NBSP;
var a; 
document.write (a);  
</script> 

But if the declaration of a is an array, the blank is returned.  
<script type= "Text/javascript" >&NBSP;
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); The 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.

<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
Array2=array1.shift ()
document.write (array2+ "<br/>"); Output 1
document.write (array1);//Output 2, 3, 4, 5
</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>

Posted from: http://blog.csdn.net/chen__jinfei/article/details/6539804, thank you

Some methods of arrays and strings in JS

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.