Summary of properties and methods of array objects in JS

Source: Internet
Author: User

One: Properties

    • Length

Length In addition to the common length of the read array, you can also modify the value of length to add or remove elements to the array, when the length value is reduced, the corresponding element is removed from the end of the array, adding the corresponding number of elements when added, but the new element is not assigned value, The initial value is undefined;

Two: Methods

detecting arrays

if (obj instanceof Array) {

Performing an array operation

}

But there is a problem with this approach. When a page is made up of frames, the window objects in different frames are different, and the array object is actually a property of the Window object, and if the array is defined in the frame, then the judgment returns False.

The following method is new in ECMAScript5, implemented in ie9+, and newer browsers

if (Array.isarray (obj)) {

Performing an array operation

}

For browsers that do not implement this method, to accurately detect an array, you can use the following methods: (This method is still part of the understanding, updated later)

2. Conversion method

ToString (), tolocalestring ()

When you execute both methods, the array executes both methods for each element. The resulting string is separated by a, (comma) of each element.

Join ("|");

Use the Join () method to select a variety of delimiters, nothing to write, by default,

ValueOf ()

3. Stack method

The features of the stack (last on first out), LIFO.

The push (Value1,value2) method mimics the input, adds elements to the end of the array, can be multiple, or can use Arr[arr.length]=value;

The Arr.pop () method mimics, reads an element from the end of the array, and reads one at a time;

4. Queue method

Characteristics of the queue (first in the Out), FIFO

Add elements to the end of the array by push () as with the Stack method;

Arr.shift (); reads an element from the front of the array, reading one at a time;

   ECMAScript also provides the Arr.unshift (Value1,value2) method, which allows you to add several elements to the front of the array.

Therefore, there are two methods of implementing the stack and the method of implementing the queue.

Push (value1,value2) pop () LIFO stack

Push (Value1,value2) Shift () FIFO queue

Unshift (value1,value2) pop () FIFO queue

Unshift (value1,value2) shift () LIFO stack

in the above method, the added element returns the new length of the array. However, Unshift (value1,value2) returns undefined in IE7 (inclusive) and the following versions;

Reads an element, returns a read element, and the type is the same as an element. and the length of the array is reduced by one;

5. Reorder Methods

Arr.reverse () reversed, arr.sort () sorted by default, sorted by ASCII code value.

The Arr.sort () method can pass in a parameter that represents a comparison rule. For example: ASC (Ascending), desc (descending), can also define their own names, common and compare;

To illustrate:

function ASC (value1,value2) {

if (value1<value2) {

return-1;

}else if (value1 > value2) {

Return 1;//this place as long as the number of >0, the exchange of two number of orders;

}else{

return 0;

}

}

In descending order, 1 and 1 in the ASC function can be reversed;

If you say that the elements in the array are numeric types, you can use the following shorthand rules.

function Compare (value1,value2) {

return value2-value1;

}

6. How to Operate

①arr.concat (value1,value2)

This method first creates a copy of the array and then adds the passed in parameter to the end of the copy, and if there is an array in the argument, adds all the elements in the array, and then returns the new array, but the original array, arr, does not change;

    ② arr.sliice (3,7);

This method reads the array subscript 3 (including 3) to 7 (not including 7) between the elements, forming a new array to return. Where 7 can be omitted, then all elements from 3 to the end are returned. The original array does not change.

Arr.slice ( -3,-1);

When a negative value is passed in the slice method, it is read from the end of the array, and the last one is-1, then descending in turn. As with positive values-1 can also be omitted and then read to the front end.

When the second argument is less than the first argument, an empty array is returned.

③splice (parameter 1, parameter 2, Parameter 3, ...) This method is the most powerful method in the array, but it is used primarily to add elements to the middle of the array;

      Parameter 1, is the array to replace the subscript of the element, parameter 2, is the number to replace, the future parameter is to insert into the element;

      "1" Delete

Set "Parameter 1" to delete the element's following table, "Parameter 2" is set to delete the number, the insertion element is not written , it implements the deletion of the specified position of the element; Arr.splice (2,2);        

var t = [0,1,2,3,4,5,6,7,8,9];
var d = t.splice (2,2);
document.write (t.tostring () + "<br/>");//0,1,4,5,6,7,8,9
document.write (D.tostring ());//2,3

"2" Insert

This is mainly to set "Parameter 2" to 0, Arr.splice (2,0,11,22);     

var t = [0,1,2,3,4,5,6,7,8,9];
var d = t.splice (2,0,11,22);
document.write (t.tostring () + "<br/>");//0,1,11,22,2,3,4,5,6,7,8,9
document.write (D.tostring ());//Empty array

"3" replacement

This is the most complete of all the parameters written.

Splice (2,1,22,33,44); The number of replacements may not be equal to the number of deletions;

    This method returns an array of elements that are deleted, and returns an empty array when no elements are deleted;

7. Location method

①arr.indexof ("element", Start_index), element is the item to find, Start_index is the starting subscript, and the element containing the start_index subscript. Only the first corresponding subscript is found, no return-1;

②arr.lastindexof ("element", Start_index); ibid., but it is from the back to the next number; it is also read only to the first corresponding subscript;

8. Iterative Methods

There are a variety of iterative methods in the array, great ...

All iteration methods contain two parameters, the first one is the function to be executed for each element, and the second is the scope object (which can be omitted).

The format of the function is:

function (Item,index,array); item: Array item, array item subscript, array object itself;

Every (), where each element returns true after the function is executed, the function returns true;

Some (), any one of which returns true, returns true;

Filter (), after each element runs the function, returns the array of true entries;

Map (), each element runs a function that makes up a new array of the results returned by each element;

ForEach (), no return value. Similar to For loop

9. Merge method

The two methods below contain two parameters, the first is the function that each item of the array calls, and the second is the initial value that is the base of the merge. It then returns a final value.

The function format to execute is:

function (Pre,cur,index,array) {

Each of the actions performed;

}

var t = [0,1,2,3,4,5,6,7,8,9];
Alert (T.reduce (function (Pre,cur,index,array) {
return pre+cur;
});//45

The value returned after each item is executed as the pre for the latter item;

       

       

Summary of properties and methods of array objects 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.