Additions and deletions to the array

push()

push () method to add one or more elements to the end of the array, and returns the new length.

var arr = ['a','b','c','d']

arr.push("e")  // a,b,c,d,e
arr.push("e", "f") //  a,b,c,d,e,f

pop()

pop () method removes and returns the last element of the array.

 

var arr = [ 'A', 'B', 'C', 'D' ] 
arr.pop () // delete D, arr = [ 'A', 'B', 'C'] 
// if arr = [], arr.pop () returns undefined

 

 

unshift()

unshift () method to add one or more elements to the beginning of the array, and returns the new length.

var arr = ['a','b','c','d']

arr.unshift("e")  // e,a,b,c,d
arr.unshift("e", "f") //  e,f,a,b,c,d

shift()

shift () method is used to remove the first element of the array from which, and returns the value of the first element.

var arr = [ 'A', 'B', 'C', 'D' ] 
arr.shift () // Delete A, arr = [ 'B', 'C', 'D'] 
// if arr = [], arr.shift () returns undefined

 

 

splice()

splice () method to add / remove items from the array to /, then return items are deleted. This method changes the original array.

arrayObject.splice(index,howmany,item1,.....,itemX)

index: Required. Integer, regulations add / remove items of location, may require the use of a negative position from the end of the array.

howmany: Required. The number of items to delete. If set to 0, it will not remove items.

 item1, ..., itemX: Optional. New projects added to the array.

var ARR = [ 'A', 'B', 'C', 'D', 'E', 'F' ] 
arr.splice ( 2)   // Delete a, after the elements b, arr = [ 'a' , 'B'] 
arr.splice (2,3) //   delete a, 3 after the elements B, ARR = [ 'a', 'B', 'F'] 
arr.splice (2,3, 'a ') //   delete a, 3 after the elements, b, and using "a" instead of, arr = [' a ', ' b ',' a ',' f ']

 

replace()

replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.

stringObject.replace(regexp/substr,replacement)

regexp / substr: Required. RegExp object to replace or substring predetermined pattern.

Note, if the value is a string, then it is as a direct amount to be retrieved text mode, without first being converted into RegExp object.

replacement: Required. A string value. Alternatively the predetermined function generates replacement text or text.

 

// replacement character 
var STR = " ABCDEFG " 
str.replace ( / C /, " ha " )     //   ab & DEFG ha 

@ regular matching 
var STR = " ABC de FG " 
str.replace ( / \ S / G , " ha ha ha " )     // abc ha ha ha ha ha ha de fg

 

 

 

Guess you like

Origin www.cnblogs.com/lvsk/p/11995666.html