JS Arrays Object Array method

Source: Internet
Author: User

1, concat () concatenate two or more arrays. The method does not alter the existing array, but only returns a copy of the concatenated array.

var arr = [1, 2, 3];

var arr1 = [11, 22, 33];

document.write (arr.concat (4, 5, arr1));

Output Result: 1,2,3,4,5,11,22,33

2. Join () puts all the elements of the array into a string. element is delimited by the specified delimiter.

var arr = [' Item 1 ', ' Item 2 ', ' Item 3 '];

var list = ' <ul><li> ' + arr.join (' </li><li> ') + ' </li></ul> ';

List result: ' <ul><li>item 1</li><li>item 2</li><li>item 3</li></ul> '

This is by far the quickest method! Using native code, such as join (), is usually much faster than non-native, regardless of what is done inside the System.

3, pop () Delete and return the last element of the array

The pop () method removes the last element of the array, reducing the length of the array by 1 and returning the value of the element it Deletes.

If the array is already empty, pop () does not change the array and returns the undefined Value.

var arr = ["George", "John", "Thomas"];

document.write (arr + "<br/>");

document.write (arr.pop () + "<br/>");

document.write (arr);

Output result:
George,john,thomas
Thomas
George,john

4. Push () adds one or more elements to the end of the array and returns the new length
var arr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");
document.write (arr.push ("James") + "<br/>");
document.write (arr);

Output result:
George,john,thomas
4
George,john,thomas,james

5, shift () Delete and return the first element of the array
var arr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");

document.write (arr.shift () + "<br/>");

document.write (arr);

Output result:
George,john,thomas

George

John,thomas

6, Unshift () adds one or more elements to the beginning of the array and returns the new length
Vararr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");
document.write (arr.unshift ("James") + "<br/>");
document.write (arr);

Output result:
George,john,thomas
4
James,george,john,thomas

7, Reverse () Reverses the order of the elements in the array
var arr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");

document.write (arr.reverse ());

Output result:
George,john,thomas
Thomas,john,george

8. Sort () Sorts the elements of the Array. A reference to an array. Note that the array is sorted on the original array, and no replicas are generated.

The method is sorted by default in the order of character encoding (ASCII).
var arr = new Array (6);
arr[0] = "John";  arr[1] = "George"; arr[2] = "Thomas";
document.write (arr + "<br/>");

document.write (arr.sort ());
Output result:
John,george,thomas
George,john,thomas

Sort () is not sorted by numeric size we think, and if you want to sort by numeric size, you need to change the default sorting method and specify the collation Yourself.

What if you want to sort in descending order? Change the collation to: function (a, b) {return b-a;}

9, Slice (start,end) Returns the selected element from an existing array.

Note that the method does not modify the array, but instead returns a Subarray
var arr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");
document.write (arr.slice (1) + "<br/>"); Truncate to the end of the array starting with the first element
document.write (arr);

Output result:
George,john,thomas
John,thomas
George,john,thomas

10. Splice () Delete the element and add a new element to the array

The splice () method is different from the slice () method, and the splice () method modifies the array directly

(1) Delete the array element of the specified range:
var arr = new Array (6);
arr[0] = "George";     arr[1] = "John"; arr[2]= "Thomas";

arr[3] = "James";   arr[4] = "adrew"; arr[5] = "Martin";

document.write (arr + "<br/>");
Arr.splice (2, 3); Three array elements after the third element is removed (contains the third Element)
document.write (arr);
Output result:
George,john,thomas,james,adrew,martin
George,john,martin

(2) Insert the specified element (unlimited number of elements) starting from the specified subscript:
var arr = new Array (6);
arr[0] = "George";     arr[1] = "John"; arr[2] = "Thomas";

arr[3] = "James";    arr[4] = "adrew"; arr[5] = "Martin";

document.write (arr + "<br/>");
Arr.splice (2, 0, "William", "JACK"); Insert "William" before the third element, "JACK"
document.write (arr);
Output result:
George,john,thomas,james,adrew,martin
George,john,william,jack,thomas,james,adrew,martin
(3) Delete the array element of the specified range and replace it with the specified element (unlimited number of elements):
var arr = new Array (6);
arr[0] = "George";      arr[1] = "John"; arr[2] = "Thomas";

arr[3] = "James";    arr[4] = "adrew"; arr[5]= "Martin";

document.write (arr + "<br/>");
Arr.splice (2,3, "William", "JACK"); Delete the next three array elements of the third element (containing the third element) and replace it with "William", "JACK"


Other methods:


ValueOf () returns the array itself.

IndexOf () Returns the position of the specified element in the array, or 1 if it does not appear.

var arr =[' a ', ' b ', ' C ';

Arr.indexof (' b ')//1

Arr.indexof (' y ')//-1

The IndexOf method can also accept the second parameter, which indicates where the search Begins.

[' a ', ' b ', ' C '].indexof (' a ', 1)//-1

The above code searches for character a from position 1th and results to-1, indicating no Search.

Tosource () Returns the source code of the Object.

ToString () Returns the string form of the Array.

var arr =[1, 2, 3];

Arr.tostring ()//"the"

toLocaleString () converts the array to a local array and returns the Result.

Map () All members of the array call a function in turn, returning a new array based on the result of the Function.

Varnumbers = [1, 2, 3];

Numbers.map (function (n) {return n + 1;}); /[2, 3, 4]

numbers//[1, 2, 3]

In the above code, all members of the numbers array are added with 1, a new array is returned, and the original array is Unchanged.

The filter () parameter is a function in which all array members execute the function sequentially, returning a new array of members that return a result of True.

The method does not change the original array.

var arr =[1, 2, 3, 4, 5]

Arr.filter (function (elem) {return (elem > 3);}) [4, 5]

JS Arrays Object Array method

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.