js array of methods and classification

 

Method name The corresponding version Features Whether to change the original array
pop() ES5- Delete the last digit, and returns the deleted data Y
shift() ES5- Delete the first one, and returns the deleted data Y
unshift() ES5- Add one or more data in the first place, returns the length Y
push() ES5- Add one or more data in the last one, the length of the return Y
reverse() ES5- Reverse list, returns the result Y
sort() ES5- Sort (character rules), returns the result Y
splice() ES5- Delete the specified location, and replaced, return the deleted data Y
toString() ES5- Directly into a string and returns n
valueOf() ES5- Returns an array of object's original value n
indexOf() ES5 Query and return data index n
lastIndexOf() ES5 Reverse query and return data index n
forEach() ES5

Parameters for the callback function will traverse an array of all the items, the callback function takes three parameters,

Respectively, value, index, self; forEach no return value

n
map() ES5 With forEach, while the callback function returns data to form a new array returned by the map n
filter() ES5 With forEach, while the callback function returns a Boolean value of true data returned by the filter to form a new array n
every() ES5 With forEach, while the callback function returns a Boolean value, all true, returned by every true n
some() ES5 With forEach, while the callback function returns a Boolean value, as long as one of the true, returned by some true n
reduce() ES5 Merge, all with forEach, iterative array and constructs a final value returned by reduce n
reduceRight() ES5 Reverse merger, all with forEach, iterative array and constructs a final value returned by reduceRight n
concat() ES5- Data after the merge array, and returns the merged n
join() ES5- Delimiters, and returns the array into a string n
slice() ES5- Array taken specified position, and returns n

An operation to change so that the original array
 
1. increasing array
  unshift(data1,data2...) 
  Increasing the number of elements to the first array and returns the new length
var arr = [1,2,3];
var str = arr.unshift(0,0,0);       
console.log(str);         //6(length)
console.log(arr);         // [0, 0, 0, 1, 2, 3]

   push(data1,data2...)

  Increasing the number of array elements to the last one, returns the new length

var arr = [1,2,3];
var str = arr.push(6,6,6);      
console.log(str);        //6(length)
console.log(arr); //  [1, 2, 3, 6, 6, 6]

 2. Delete the array

  shift()

  Remove and return the foremost element of an array

var arr = [1,2,3 ]; 
console.log (arr.pop ());     // 1 (元素) 
console.log (arr);           // [2,3]

  pop()

  Remove and return the last element of an array

var arr = [1,2,3 ]; 
console.log (arr.pop ());     // 3 (元素) 
console.log (arr);           // [1,2]

 3. To change the array

  splice (start, n, data1, data2 ...) optional parameters

  Delete elements in the array, and add new elements
 var arr = ["A","B","C","D","E"];    
console.log(arr.splice(2,0,"a","b"));  //[]
console.log(arr);    //["A", "B", "a", "b", "C", "D", "E"] 原数组改变

 

  sort()

  Sort the array of elements (characters method), the default is ascending

var ARR = [ . 4 , 2 , . 3 , . 5 , . 1 ]; 
the console.log (arr.sort ());     // [. 1, 2,. 3,. 4,. 5] 
the console.log (ARR);            // [. 1 , 2, 3, 4, 5] to change the original array

  reserve()

  Reverse the order of elements in the array

var ARR = [ . 1 , 2 , . 3 ]; 
the console.log (arr.reverse ());      // [3,2,1] 
the console.log (ARR);                // [3,2,1] changes the original array

 

II. Does not change the original array
1.slice(m,n)
 M start bits taken from the n-bit elements
When n omitted, or n = 0, until the last one element selected;
m <0 or n <0, the last bit indicates the start date
 
var arr = ["A","B","C","D","E"];
console.log(arr.slice(1,3));         //["B","C"]
console.log(arr.slice(1));           //["B","C","D","E"]
console.log(arr.slice(-4,-1));       //["B","C","D"]
console.log(arr.slice(-2));          //["D","E"]
console.log(arr.slice(1,-2));        //["B","C"]
console.log(arr);                    //["A","B","C","D","E"]

 

2.toString()
  The array is converted to character
var arr=["夜","的","第","七","章"] ;
var str=arr.toString();
console.log(str);

 Methods update ...

 

 

Guess you like

Origin www.cnblogs.com/mutuo/p/11440005.html