Js deletions change search array

array

Additions and deletions to change the array:

  • push a push from behind the array element or elements

var ARR = [l, 2,3 ]; 
// Returns: after modifying the length of the array 
arr.push (4,5,6);

 

  • The last element of the array pop deleted

// POP array of methods for removing the last element of the array 
var ARR = [l, 2,3 ]; 
// return the element to be deleted; 
arr.pop ();

 

  • unshift add one or more elements from the front of the array

var ARR = [l, 2,3 ]; 
// Returns: length of the array is modified 
arr.unshift (4,5,6);

 

  • shift means for removing the first element of the array

// Shift method for first array element of the array to remove 
var ARR = [l, 2,3 ]; 
// return the element to be deleted; 
arr.shift ();

 

  • splice: the array can be in any position of additions and deletions of

// splice array of methods for the removal of the array from the specified location, additions, substitutions of elements 
var ARR = [ 'A', 'B', 'C', 'D', 'E' ]; 
// original array operation 
@ action: 3 is removed from the index beginning, removing a total of elements, 
// returns: the element array is removed 
arr.splice (3,1 ); 
the console.log (ARR); 
// c is added after the two elements 7 and 8 
@ action: Add 3 starts from index 0 to remove elements, the addition of 7,8; 
// returns: an empty array 
// operation original array; 
arr.splice ( 3,0,7,8 ); 
// action: replacing the start index 1, a total replacement, replacement with 0; 
// returns: an array of elements that were replaced 
arr.splice (1,1,0 ) ; 
the console.log (ARR);

 

String Huzhuan

  • join the multi-element array is used to specify the separator into a string

var ARR = [ 'Bei', 'Guan', 'Zhang' ];
 var STR = arr.join ( '|' ); 
the console.log (STR);   //   Bei | Guan | Zhang

 

  • The method of split string: forwarding number followed by the character of the partition

// This method is used to specify a string of symbols into arrays 
var STR = 'Bei | Guan | Zhang' ;
 var ARR = str.split ( '|' ); 
the console.log (ARR);

 

Finding Elements

  • indexOf: Find an index based on the element, if the element in the array, the index returns, otherwise it returns -1 to find the internal elements in the array is not

var arr = [10,20,30]
console.log(arr.indexOf(30));  // 2
console.log(arr.indexOf(40));  // -1

 

  • Method for findIndex lookup index of the first element satisfies the condition, and if not, it returns -1

var ARR = [10, 20 is, 30 ];
 var RES1 = arr.findIndex ( function (Item) {
   return Item> = 20 is ; 
}); 
// return the first element satisfies the condition index 
console.log (res1 );   
var RES2 = arr.findIndex ( function (Item) {
   return Item> = 50 ; 
}); 
// -1 
the console.log (RES2);

 

 

Guess you like

Origin www.cnblogs.com/lwa1999/p/11617193.html