JavaScript stuff (i) JavaScript array usage Summary (1)

Source: Internet
Author: User
Tags array length javascript array

/*1, JS arrays, like arrays in other languages, are an ordered list of data, but the difference is that each item of the JS array can hold any type of data. And the size of the JS array can be dynamically adjusted. 2. Create array: *///(1) Create an array using the array constructor://var names = new Array ();//Create an array of length=30//var names = new Array (30);  If the array length is known beforehand, the numerical//alert (names.length) can be passed directly. 30//can also be passed as an array constructor to the items that the arrays should contain://var names2 = new Array ("Zhangsan", "Lisi", "Wangwu"),//alert (Names2);  zhangsan,lisi,wangwu//passing a value to the array constructor can also create an array//var ages = new Array (2); Create an array with two items//var ages = new Array ("11"); Create an array that contains 1 items, or the string "11"//You can omit the new operator//var books = array (2);//Create an array with two items//var books = Array ("BOOKS1"), create a string "books" that contains 1 items  Array//(2) creates an array using array literal notation: The array literal is represented by a pair of square brackets containing array items, and multiple array items are separated by commas//var names = ["Zhangsan", "Lisi", "Wangwu"]; Create an array containing 3 strings//var names = []; Create an empty array//The result of adding a comma to the last item in the array://In other browsers will create an array containing 2 items with values of "Zhangsan", "Lisi"//in IE, names will create 3 items with values of "Zhangsan", "Lisi", Undefined array,//Because the IE8 and previous version of the ECMAScript implementation in the array literal existence bug//var names = ["Zhangsan", "Lisi",];//alert (names); IE9 error, IE8 and following: Zhangsan,lisi,//alert (names.length);//Like this ellipsis, each entry is a undefined value. var names = [,,,,,];//alert (names.length);  5: Create an array containing 5 items in and other browsers//alert (names.length); 6: Create an array with 6 items in the IE version//alert (names[5]);  undefined//read set array value//var colors = ["Red", "green", "Blue"];//alert (Colors[0]);  Display the first item//colors[2] = "Pink"; Modification of the third//alert (colors); RED,GREEN,PINK//COLORS[3] = "BLACK"; Add fourth, set the index of a value beyond the array length, the array length is automatically added 1//alert (colors); RED,GREEN,PINK,BLACK//3, Length property: >=0, the Length property is not read-only, you can remove an item from the end of the array by setting the Length property, or add an item to the array//var colors = ["Red", "  Green "," blue "];//colors.length = 2;  Alert (colors[2]);  Undefined//var colors = ["Red", "green", "blue"];//colors.length = 4;  Alert (colors[3]); Undefined//var colors = ["Red", "green", "blue"];//colors[colors.length] = "BLACK";//colors[colors.length] = "Pink";  /alert (colors); Red,green,blue,black,pink//var colors = ["Red", "green", "blue"]//colors[99] = "BLACK";//alert (Colors.length); COLORS[3] to color[98] are UNDEFINED//4, array type//for a Web page or a global scope, instanceof assumes a single global execution environment//var colors = ["Red", "green" , "blue"];//var obj = {};//alert (colors instAnceof Array); True//alert (obj instanceof Array); false//for a Web page that contains multiple frames, there are several different global execution environments, with several different versions of the array constructor,//Then an array passed from one frame to another, and the array in the passed-in and the second frame have different constructs// To solve this problem, the Array.isarray () method must be used//var colors = ["Red", "green", "blue"];//var obj = {};//alert (Array.isarray (colors)); True//alert (Array.isarray (obj)); false//Compatibility: Browsers that support the IsArray method are: ie9+, other browsers//5, ToString (): Returns the array each item is separated by a comma-delimited string, calling the ToString () method of each item of the array//valueof (): Returns an array// var colors = ["Red", "green", "Blue"];//alert ("tostring=" +colors.tostring ()); Red,green,blue//alert (colors); Alert receives a string argument, which is passed to it by an array, so the ToString () method//alert ("valueof=" +colors.valueof ()) of the array is called in the back. Alert (Array.isarray (colors.valueof ())); True//tolocalestring (): Returns the comma-delimited string for each item of the array, calling the toLocaleString () method of each item instead of the ToString () method/*var Book1 = {tostring: function () {return "book1.tostring ()";},tolocalestring:function () {return "book1.tolocalestring ()";}}; var book2 = {Tostring:function () {return "book2.tostring ()";},tolocalestring:function () {return "book2.tolocalestring ()";}}; var book = [Book1,book2];alert (book); Book1.tostring (), book2.tostring () alert (book.tostring); function toString () {[Natice Code]}alert (Book.tostring ());//book1.tostring (), book2.tostring () alert ( Book.tolocalestring ());//book1.tolocalestring (), book2.tolocalestring () *///join (): receives a parameter as a delimiter, returns a string delimited by a delimiter,// var colors = ["red^", "green^", "blue^"];//alert (Colors.join (', '));//red^,green^,blue^//alert (Colors.join (' | ')); /red^|green^|blue^//alert (Colors.join ()); The default is comma delimited: Red^,green^,blue^//alert (colors.join (undefined));//displayed in ie8+ and other browsers: Red^,green^,blue^//alert ( Colors.join (undefined)); Shown in IE7 and the following versions: red^undefinedgreen^undefinedblue^//If an item in the array has a value of null or undefined, the value is in join () \valueof () \tostring () \ toLocaleString () returns an empty string//var names = ["Zhangsan", null,undefined, "Lisi"];//alert (names); Zhangsan,,, Lisi//push (): Receives any number of arguments, adds to the end of the array, returns the modified array length//var names = new Array (),//var num = Names.push ("Zhangsan", "Lisi ");//alert (num); 2//num = Names.push ("Wangwu");//alert (num); 3//var item = Names.pop ();//aLert (item);  Wangwu//shift (): Removes the first item of the divisor group and returns//var colors = ["Red", "green", "blue"];//var item = Colors.shift (); Remove the first item and return//alert (item);//alert (colors.length); 2//unshift (): Adds any item to the front of the array and returns the new length of the array, for IE7 and earlier, Unshift returns undefined instead of the array length//var colors = ["Red", "green", "Blue"];// var count = Colors.unshift ("AAA", "BBB");//alert (colors);//"AAA", "BBB", "Red", "green", "Blue"//alert (count);//5

JavaScript stuff (i) JavaScript array usage Summary (1)

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.