Friday, again will be off work, just a "JavaScript Advanced program Design" Array This piece looked again, deepened the memory. Today to continue to practice LIANBI, hey! (Write down something that you don't remember deeply)
I. Definition of arrays (array definitions are divided into two types)
Method One:
1var color=new Array (num),//num as a number, (of course, writable, not writable) indicates the length of the array
var color=new array ("Red", "yellow");//Direct definition of array contents
Method Two:
2.var color=[];//Of course can also be defined when writing the array contents directly ["Red", "yellow"]
var color=["Yellow", "red"]
Two. Detecting arrays
Method One:
instanceof // returns TRUE or False (the method itself is not a problem, but it may be a mistake when the page contains multiple frames);
Method Two:
Es5 New Method IsArray, through Array.isarray (color), also return the true and False value, of course, many think of this has a compatible new problem exists finally can consider the use of Object.prototype.toString.call ( color)= = = "[Object Array]" (Tostring.call (color)) to determine whether an array
Three methods
1. Push is the end of the array insertion, unshift head insertion (both a head, a tail)
2. Pop data tail Delete, shift array Header Delete
the above 4 methods will change the contents of the original array, the length (the top is not elaborate on the code to practice it)
3. In detail slice and splice These two I always remember not clear or old forget
Slice can accept the start and end positions of one or two parameters (Start,end). This method is used to intercept the parameters from the array, without changing the original array, of course. For example:
varnum=[1,2,3,4,5]; Num.slice (2,4);//[3,4];Console.log (num);//[1,2,3,4,5]Splice This method has three ways to use (change array)
1Remove two parameters start,length that is the start position and the length of the deletion2. Insert three parameters Start,length (delete), content (what to insert)3replace (can also be called Delete after insert) is also three parameters
Examples are as follows:varnum=[1,2,3,4];varDel=num.splice (0,2);//[up]Console.log (num);//[3,4];varInsert=num.splice (1, 0, "5", "6");//return NULL The second parameter is 0Console.log (num);//[3,5,6,4]varChange=num.splice (1, 1, "7", "8");//[5]Console.log (num);//[3,7,8,6,4]
Note: Slice affects the original array, splice does not affect the original array
Next, Es5 added in the Map,foreach,filter,every,some and so on
These iterative methods contain two parameters: the first method function to operate on an array object, and the second (optional argument) for a Scope object method function to pass three parameters: 1. Each object in the array, 2 in the array position, 3, the array object itself (optional). Every () returns a true and False value for each object in the primary array, each of which evaluates to true when the last every Returns Truesome () and every, which is the same as long as there is a qualifying condition that returns an array of Truemap (). Returns the values that are required for each action function to form an array
ForEach () does not return a value primarily by using the parameter function to manipulate objects in an array
Filter () filters return an array of true (when eligible)
var num=[1,2,3,4,5];
Num.every (function (item,index) {return item>1});//False
Num.some (function (item,index) {return item>1});//true
Num.map (function (item,index) {return item*2});//[2, 4, 6, 8, 10]
Num.foreach (function (item,index) {Console.log (item*2)});
Num.filter (function (item,index) {return item>2});//[3, 4, 5]
Write to this, work, haha, not the welcome error Oh!!!
Array of those things (slice,splice,foreach,map,filter, etc.)