Javascript (1) Summary of javascript Array usage (1)
/* 1. The js array, like the arrays in other languages, is an ordered list of data, but the difference is that each item of the js array can save any type of data. In addition, the js array size can be dynamically adjusted. 2. Create an Array: * // (1) Use the Array constructor to create an Array: // var names = new Array (); // create an Array with length = 30 // var names = new Array (30); // if you know the Array length in advance, you can directly input a value // alert (names. length); // 30 // You can also pass the items that the Array should contain like an Array constructor: // var names2 = new Array ("zhangsan", "lisi ", "wangwu"); // alert (names2); // zhangsan, lisi, wangwu // pass a value to the Array constructor or create an Array // var ages = new Array (2 ); // create an Array containing two items // var ages = new Array ("11"); // create an Array containing one item, that is, the array of the string "11" // can omit the new operator // var bo Oks = Array (2); // create an Array containing two items // var books = Array ("books1"); Create an Array containing one item, that is, the array of the string "books" // (2) create an array using the array literal representation: the array literal is represented by a pair of square brackets containing the array items, separate multiple array items with commas // var names = ["zhangsan", "lisi", "wangwu"]; // create an array containing three strings // var names = []; // create an empty array // Add a comma to the last entry of the array: // two arrays containing "zhangsan" and "lisi" are created in other browsers. // in IE, names contains three items, the values are the "zhangsan", "lisi", and undefined arrays respectively, // The cause is that IE8 and earlier versions of ECMAScript have bugs in array literal quantity. // var names = ["zhangsan", "li Si ",]; // alert (names); // IE9 error, IE8 and below: zhangsan, lisi, // alert (names. length); // if this value is omitted, each item is an 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 containing 6 items in IE version // alert (names [5]); // undefined // read the set array value // var colors = ["red", "green", "blue"]; // alert (colors [0]); // display the first item // colors [2] = "pink"; // modify the third item // alert (colors); // red, green, pink // colors [3] = "black"; // Add the fourth item. If the index of a value exceeds the length of the array, the array length is automatically added with 1 // alert (colors); // red, green, pink, black // 3, length attribute:> = 0, length attribute is not read-only, you can set the length attribute to remove an item from the end of the array 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); // 100, colors [3] To color [98] are all undefined // 4. array type judgment // For a webpage 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 webpages that contain multiple frameworks, there are multiple different global execution environments, so there are multiple different versions of the Array constructor. // You can input an Array from one framework to another, the input Array and the Array in the second framework have different structures. // to solve this problem, Array must be used. isArray () method // var colors = ["red", "green", "blue"]; // var obj ={}; // alert (Array. isArray (colors); // true // alert (Array. isArray (obj); // false // compatibility: browsers that support the isArray method include IE9 +, other browsers // 5, toString (): returns a comma-separated string of each array. The toString () method // valueOf () of each array item is called. The returned array // var colors = ["red ", "green", "blue"]; // alert ("toString =" + colors. toString (); // red, green, blue // alert (colors); // alert receives string parameters and transmits them to an array, therefore, the toString () method of Array will be called later // alert ("valueOf =" + colors. valueOf (); // alert (Array. isArray (colors. valueOf (); // true // toLocaleString (): returns a string separated by commas (,) for each item in the array, and calls the toLocaleString () method for each item instead of 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 separator parameter and returns a string separated by a separator, // var colors = ["red ^", "green ^", "blue ^"]; // alert (colors. join (','); // red ^, green ^, blue ^ // alert (colors. join ('|'); // red ^ | green ^ | blue ^ // alert (colors. join (); // separated by commas by default: red ^, green ^, blue ^ // alert (colors. join (undefined); // displayed in IE8 + and other browsers: red ^, green ^, blue ^ // alert (colors. join (undefined); // displayed in IE7 and earlier versions: red ^ undefinedgreen ^ undefinedblue ^ // If a value in the array is null or undefined, the value is join () \ valueOf () \ toString () \ toLocaleString () All return an empty string // var names = ["zhangsan", null, undefined, "lisi"]; // alert (names); // zhangsan, and lisi // push (): receives any number of parameters and adds them 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 entry of the array and returns // var colors = ["red", "green ", "blue"]; // var item = colors. shift (); // remove the first item and return // alert (item); // alert (colors. length); // 2 // unshift (): Add any item on the front end of the array and return the new length of the array. For IE7 and earlier versions, unshift returns undefined instead of array length. // var colors = ["red", "green", "blue"]; // var count = colors. unshift ("aaa", "bbb"); // alert (colors); // "aaa", "bbb", "red", "green ", "blue" // alert (count); // 5