JavaScript Array-Array method summary (recommended), javascriptarray

Source: Internet
Author: User
Tags javascript array

JavaScript Array-Array method summary (recommended), javascriptarray

The Array type in JavaScript is frequently used. The Array type also provides many methods to meet our needs. Let's summarize the following:

1. method for creating an Array

Var colors = new Array ();

Var colors = new Array (3); // create an Array with a length of 3

Var colors = new Array ("red", "blue") // create an Array ["red", "blue"]

Of course, the new above can be omitted without writing, such as var colors = Array ("red ");

2. directly use the array literal

Var colors = ["red", "blue", "green"];

2. method 1 of Array

Var colors = ["red", "blue", "green"];

1. Obtain the array length colors. length; // 3

2. Access the second item of the array colors [1]; // blue

3. Change the second item of data colors [1] = "black"; // ["red", "black", "green"]

4. check whether it is an Array of colors instanceof Array; // true

5. colors. toString (); // by default, the output strings red, blue, and green are separated by commas.

6. colors. join ("|"); // custom output string separated by "|" red | blue | green

7. colors. push ("brown") // Add an item to the end of the array

8. colors. pop () // delete an item from the end of the array

9. colors. shift () // Delete the first entry of the array and obtain the value.

10. colors. unshift ("k1", "k2") // Insert the two items to the front of the array. ["k1", "k2", "red", "blue ", "green"];

11. colors. reverse () // reverse the order of the array

12. colors. sort () or colors. sort ([func]);

13. concat () returns a new array without affecting the original array colors. concat () or colors. concat ("k1 ");

14. slice (begin, end) copies the data from the array subscript begin to the end, excluding the subscript end. If it is slice (begin), it is from the subscript begin to the end of the array.

15. splice

Splice () // Delete two items of the array starting from subscript 0

Splice (, "k1", "k2") deletes 0 items from subscript 2, and then inserts two items from here

Splice (, "k1") // delete an item from subscript 2, and insert an item from here

16. indexOf ("item") // search for an item starting from the array header. After finding the item, return the value of the lower mark. If no value is found, return-1.

17. lastIndexOf ("item") // search for an item starting from the end of the array. If it is found, the following value is returned. If it is not found,-1 is returned.

3. Array Method 2: iteration method (ECMAScript5)

1. every (): returns true if each function of the array is set to true (the original array is not affected)

Var numbers = [1, 2, 3, 2, 1]; // you can determine whether each number is greater than 2var flag = numbers. every (function (item, index, array) {return item> 2 ;});

2. filter (): run the given function for each item in the array and return the item with the function set to true (the original array is not affected)

Var numbers = [1, 2, 3, 2, 1]; // return the var array = numbers of an item greater than 2. filter (function (item, index, array) {return item> 2 ;});

3. forEach (): executes the given function for each item in the array without returning the value (the original array is not affected)

Var numbers = [1, 2, 3, 2, 1]; // output the square numbers of each item. forEach (function (item, index, array) {console. log (item * 2 );});

4. map (): executes the given function for each item of the array and returns an array composed of the result after each function call (the original array is not affected)

Var numbers = [1, 2, 3, 2, 1]; // returns the square var array of each item = numbers. map (function (item, index, array) {return item * item ;});

5. some (): executes the given function for each item of the array. If one item returns true, true is returned.

var numbers=[1,2,3,2,1];var flag=numbers.some(function (item,index,array) {  return item>2});

3. Array method 3: ECMAScript5)

1. The reduce () method starts from the first row of the array and traverses one by one to the end.

2. The reduceRight () method starts from the last entry of the array and traverses forward one by one.

Var numbers = [1, 2, 4, 5]; var result = numbers. reduce (function (prev, cur, index, array) {// prev: The result of the previous operation. It is the first item of the number at the beginning. // cur: current entry of the array // index: subscript of the current array // array: array for which this operation is performed, which is currently the numbers console. log ("prev:" + prev); console. log ("cur:" + cur); console. log ("index:" + index); console. log ("array:" + array); console. log ("="); return prev + cur ;});

The summary of the above JavaScript Array-Array method (recommended) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

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.