JavaScript-- array method

End declare an array of learning, assignments, type of judgment, we begin to look at what are the array method, but in order to know what is the use of these methods, I will for example be operated.

First, the array into a string (not change the value of the original array)

1, toString () method: We can make each array into a string separated by commas;

var HHH = [ . 1 , 2 , . 3 ];
 / * conversion method * / 
the console.log (hhh.toString ()); // l, 2,3

2, join () method: You can specify your own strings, and if we pass the "*", then the final string is separated with * string;

var HHH = [ . 1 , 2 , . 3 ];
 / * conversion method * / 
the console.log (hhh.join ( ' / ' )); // -by- 1/2/3

Second, as the stack as the operating characteristics of the data array inside the stack is LIFO so the impact is first received an array of tail - because they are coming later (stack method will change the value of the original array)

1, push () method: add entries return value is an array of length from the end of the operation of the array;

var HHH = [ . 1 , 2 , . 3 ];
 // to accept any number of items, and the item is added to the end of the data, and returns the final length of the array 
var rtnvalue = hhh.push ( ' haha ' , ' heihei ' );
console.log(hhh);//[ 1, 2, 3, 'haha', 'heihei' ]
console.log(rtnvalue);//5

2, pop () method: the value of that item is removed some items removed from the end of the array the return value;

var HHH = [ . 1 , 2 , . 3 ];
 // removed from the end of the item is removed and returned values 
var removerfromend = hhh.pop ();
console.log(removerfromend);//3
console.log(hhh);//[ 1, 2]

Third, the same operation as the queue is a FIFO queue array so the first to receive the impact of those values ​​is the beginning of the array because they are advanced to the (queue method will change the value of the original array)

1, shift () method: remove some items from the header of the array;

var HHH = [ . 1 , 2 , . 3 ];
 // removed from the top 
var ITE = hhh.shift ();
the console.log (ITE); // Returns that a 1 is removed 
the console.log (HHH); // [2,. 3]

2, unshift () method: the array from the head portion is pushed into a number of items;

var HHH = [. 1, 2,. 3 ];
 // Add the top 
var RTN = hhh.unshift ( 'Hua', 'Long' );
the console.log (RTN); // Returns the length of the array from the top after the addition. 5 
the console.log (HHH); // [ 'Hua', 'Long',. 1, 2,. 3]

Fourth, an array sorting (sorting method will change the value of the original array)

1, sort () method: the array in ascending order, and returns a sorted array. This method changes the value of the original array;

was arr = [3, 2, 6, 9, 0, 4, 5, 2, 7, 8, 8, 12 ];
arr.sort (); // default ascending 
the console.log (ARR); // [0, 12 is, 2, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 8,. 9]

2, a custom comparison function, passed as a parameter sort (method). For example, we want the array is not the default in ascending order, but goes in descending order;

var ARR = [. 3, 2,. 6,. 9, 0,. 4,. 5, 2,. 7,. 8,. 8, 12 is ];
 // custom comparison method of changing the collation sort 
function CoMP (V1, V2) {
     IF (V1 > V2) {
         return -1 ;
    } else if (v1 < v2) {
        return 1;
    } else {
        return 0;
    }
}
arr.sort(comp);
console.log(arr);//[ 12, 9, 8, 8, 7, 6, 5, 4, 3, 2, 2, 0]

3, reverse () method: the array in reverse order;

was arr = [3, 2, 6, 9, 0, 4, 5, 2, 7, 8, 8, 12 ];
arr.reverse (); // sequentially inverting 
the console.log (ARR); // [12 is,. 8,. 8,. 7, 2,. 5,. 4, 0,. 9,. 6, 2,. 3]

V. arrays (arrays splice operation method, only the original method changes the value of the array)

1, concat () method: In the end connection number array, the same original array, the method additionally returns a new array;

var ARR = [. 3, 2,. 6,. 9, 0,. 4,. 5, 2,. 7,. 8,. 8, 12 is ];
 / * Operation * / 
var newarr arr.concat = ( 'I'); // a plurality of arrays connected to the rear array 
the console.log (ARR); // [. 3, 2,. 6,. 9, 0,. 4,. 5, 2,. 7,. 8,. 8, 12 is] 
the console.log (newarr); // [3, 2, 6, 9, 0, 4, 5, 2, 7, 8, 8, 12, 'I']

2, slice (): Use the start and end values, according to the index beginning from the start (including an initial position) to the end value (no end position) Returns a new additional array;

var ARR = [. 3, 2,. 6,. 9, 0,. 4,. 5, 2,. 7,. 8,. 8, 12 is ];
 var selectorarr arr.slice = (2,. 4); // contains the start position does not include the final position 
the console.log (selectorarr); // so there will only be two 23 does not contain. 4: [. 6,. 9] 
the console.log (ARR) // [3,2,6,9,0,4,5 , 2,7,8,8,12]
was arr = [3, 2, 6, 9, 0, 4, 5, 2, 7, 8, 8, 12 ];
was selectorarr1 = arr.slice (2 );
console.log(selectorarr1);//[ 6, 9, 0, 4,  5, 2, 7, 8, 8, 12]

3, splice () method: This method takes three parameters, namely the starting position, items to be removed, from the position of the new entry is inserted; we can adjust these three parameters to achieve different effects;

// splice method add, delete, change 
var myArr = [. 1, 2,. 3,. 4,. 5,. 6 ];
 var RES = myarr.splice (2,. 3 'incorporating a', 'and insert a'); // remove from indexer at two 3 (345) and two newly inserted 
the console.log (RES); // who were removed: [. 3,. 4,. 5] 
the console.log (myArr); / / [1, 2 'incorporating a', 'and insert a',. 6] 
var myarr2 = [1, 2,. 3,. 4,. 5,. 6 ];
 var RES2 myarr2.splice = (2, 0, 'is inserted a ',' then insert a '); // remove from 0 position two and two insert 
the console.log (RES2); // not return an empty array entry is deleted 
the console.log (myarr2); // returns after the array operation [1, 2 'is inserted into a', 'then insert a', 3, 4, 5,6] 
// The second parameter is n, then n new values into an array and may be replaced 
var readyarr = [1, 2, 3, 4];
var rmv = readyarr.splice(2, 1, 'baba');
console.log(rmv);//[ 3 ]
console.log(readyarr);//[ 1, 2, 'baba', 4 ]

Six, search positions in an array (type of return value number, the operation does not change the value of the original array)

1, indexof () method: from left to right to do search. This method has two parameters, the first value is to be searched, the second is the start position; default start position to start the search is from 0 (i.e., beginning of the array);

/ * Location methods * / 
var operatearr = [. 1, 3, 2,. 7,. 8, 2 ];
 var index = operatearr.indexOf (3); // Find 3 where the subscript zero default start looking 
console.log ( index); // returns the index. 1 
the console.log (operatearr.indexOf (3, 0)); // the index 3 where to start looking for zero return. 1 
the console.log (operatearr.indexOf (3, 2)); // subscript 2 from the last start looking for did not find returns -1

2, lastindexof () method: right to left search. This method has two parameters, a first value is to be searched, the second is the start position; default is the start position to start the search from the end of the array;

/ * Location methods * / 
var operatearr = [. 1,. 3, 2,. 7,. 8, 2 ];
the console.log (operatearr.lastIndexOf ( 2)); // from the back 2 to find the default position in which starting from the last index to find a return. 5 
the console.log (operatearr.lastIndexOf (2, operatearr.length -. 1)) ; // returns 5

Seven array of iterative method (iterative method does not change the value of the original array)

1, every () method: This method returns a Boolean value eventually. Each array which will run a given function that each item will return true return true;

var useoperate = [. 1, 2,. 3,. 4,. 5,. 6,. 7 ];
 // use the every method returns true for each array each run the given function before the function returns true 
// example; Analyzing it an array with greater than 2 (to be used per every iteration than a large two will return to true) 
var Results useoperate.every = ( function (ITM, IDX, ARRS) {
     return (ITM> 2 );
});
console.log(results);//false

2, some () method: This method is also a Boolean value returned by the final. The method of each array will run a given function, the function returns true as long as an entire value is true to some function;

var useoperate = [. 1, 2,. 3,. 4,. 5,. 6,. 7 ];
 // use returns true as long as a whole some some function return value in the array for each run on a given function is the function to true 
var results2 = useoperate.some ( function (ITM, IDX, ARRS) {
     return (ITM> 2 );
});
console.log(results2);//true

3, filter () method: This method returns a new array further. Each array which will run a given function, and returns an array function returns a value of true items thereof;

var useoperate = [. 1, 2,. 3,. 4,. 5,. 6,. 7 ];
 // / fiter array using the method given for each run the function returns true last function returns an array of items consisting of 
var results3 = useoperate. filter ( function (ITM) {
     return (ITM> 2 );
});
console.log(results3);//[ 3, 4, 5, 6, 7 ]
console.log(useoperate);//[ 1, 2, 3, 4, 5, 6, 7]

. 4, map () Method: map array method will run in each given function, after the map array method will change the return operation is completed;

var useoperate = [. 1, 2,. 3,. 4,. 5,. 6,. 7 ];
 // use map each method in the array to the last run the given function returns the array after operation of the function 
// such methods may use the map ten times larger than the array so that each 
var results4 = useoperate.map ( function (Item) {
     return Item * 10 ;
});
console.log(results4);//[ 10, 20, 30, 40, 50, 60, 70]
console.log(useoperate);//[1, 2, 3, 4, 5, 6, 7]

5, foreach () method: This method would be an array for each run a given function, its nature and for () there is no difference, this method does not return value;

var useoperate = [. 1, 2,. 3,. 4,. 5,. 6,. 7 ];
 // use foreach method is only one operation for each array for a given function of the nature of the difference is not read does not return value 
useoperate.forEach ( function (t) {
    the console.log ( 'the current item:' + T);
});
Results of the:
Current item: 1 
current item: 2 
current item: 3 
current item: 4 
current item: 5 
current item: 6 
current item: 7

Guess you like

Origin www.cnblogs.com/bigbosscyb/p/12170725.html