Describes basic Js Array Operations

Source: Internet
Author: User
This article describes basic methods for Js Array Operations 1. array detection


Var newArr = [1, 2, 3]; newArr instanceof Array; // trueArray. isArray (newArr); // true // Array. isArray (newArr) only supports ie9 +, firefox 4 +, safari 5 +, opera 10.5 +, and chrome



2. Create an array


Var newArr = new Array (); // create an Array var newArr = new Array (3); // create an Array and specify the length var newArr = new Array (1, 2, [3, 4]); // create an array and assign values


3. Add Elements

Var newArr = new Array (); // create an Array var newArr = new Array (3); // create an Array and specify the length var newArr = new Array (1, 2, [3, 4newArr. push ('A'); // 6 newArr; // [1, 2, 3, 4, 5, 'a'] newArr. unshift ('B'); // 7 newArr; // ['B', 1, 2, 3, 4, 5, 'a'] newArr. splice (2, 0, 'C'); // [] newArr; // ["B", 1, "C", 2, 3, 4, 5, "A"]); // creates an array and assigns A value.

4. Delete Elements

var newArr = [1,2,3,4,5];newArr.pop();  //5newArr;  // [1,2,3,4]newArr.shift(); //1newArr;  // [2,3,4]newArr.splice(1,2); //[3,4]newArr;  //[2]

5. truncate and merge elements


var newArr = ['A','B','C','D','E'];newArr.slice(2); //["C", "D", "E"]newArr.slice(1,4); //["B", "C", "D"]newArr.concat('M');  //["A", "B", "C", "D", "E", "M"]


6. Copy an array


var newArr = ['A','B','C','D','E'];newArr.slice(0); //["A", "B", "C", "D", "E"]newArr.concat();  //["A", "B", "C", "D", "E"]


7. Sort Arrays


var newArr = ['A','C','B','D','M'];newArr.reverse();  //["M", "D", "B", "C", "A"]newArr.sort();  //"A", "B", "C", "D", "M"]


8. String Conversion of Arrays

Var newArr = [1, 2, 3]; newArr. join (','); // "1, 2, 3" newArr. toString (); // "1, 2, 3" newArr. toLocaleString (); // "1, 2, 3" newArr. valueOf (); // [, 3] // toLocaleString, toString, valueOf: can be considered as a special use of join, not commonly used

9. Search for Arrays

Var newArr = [1, 2, 3, 4, 5, 4, 3, 2, 3, 4]; newArr. indexOf (3); // the location where element 3 appears for the first time: 2newArr. indexOf (3, 4); // start from position 3 to find the position where element 4 appears for the first time: 6newArr. lastIndexOf (3); // the position where element 3 is last displayed: 8newArr. lastIndexOf (2, 4); // start from position 4 to find the last location where element 2 appears: 1var person = {name: "lili"}, person1 = [{name: "lili"}], person2 = [person]; person1.indexOf (person); // The returned-1 value must be strictly equal, not just the value equal to person2.indexOf (person ); // return 0 // indexOf () from the past to the next query. The value-1 is not found. // lastIndexOf () is found from the next query. The value-1 is not found.

Note: you can find the materials for your reference only. Thank you ~~

The above is a detailed description of the basic methods for Js Array Operations. For more information, see other related articles in the first PHP community!

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.