Several methods work string and array operations frequently used

String conversion

toString()

String()

----------------------------------------------------

The string into an array

var str = 'a,string'

var arr = str.split ( ',') // array of characters into

console.log (arr) // Results [ 'a', 'string']

-----------------------------------------------------

To delete a specific character string

var str = 'abc'

var msg = str.replace('a','')

console.log (msg) // results 'bc'

------------------------------------------------------

Interception string

var str = 'ABCD'

var substr = str.substring (0,2) // results 'abc'

------------------------------------------------------

Get file extension

var txt = 'abc.jpg'

var suff = txt.substr(txt.lastIndexOf('.'))

console.log (suff) // results '.jpg'

------------------------------------------------------

Splicing two arrays

where, R1 = [ '1', '2']

was Arr 2 = [ '3', '4']

arr1.push.apply(arr1,arr2)

console.log (arr1) // Results [ '1', '2', '3', '4']

------------------------------------------------------

Converting the object into an array of strings JSON

JSON.stringify(arr)

------------------------------------------------------

Adds an array

unshift () // add front

push () // add back

 -------------------------------------------------------------

To delete one or more of the array

splice(1,1)

 -------------------------------------------------------------

Loop array

forEach()

 

Guess you like

Origin www.cnblogs.com/zpfqi/p/11243085.html