An array of methods

What is an array?

Arrays are objects, wrapped enclosed within brackets, the index is incremented from scratch, there are stored the array length property length

ary [0] first obtain

ary [aru.length-1] obtaining a final

Arrays can have multi-dimensional arrays

var ary=[
 {name:'a'},
 {name:'b'}
]

An array of commonly used methods:

push: add to the end of the array, one or more parameters, the new array length after the return, the original array change
pop: Delete one end of the array, no arguments and returns deleted items, change the original array

shift: first delete the array, without parameters, return item to remove, and change the original array (array-based shift delete the first item, each item must be an original behind the index (a prerequisite to minus), if the array is long, consumption is relatively large)

unshift: To the beginning of the array, one or more parameters, returns the length of the new array, the original array change

reverse: the array upside down, no arguments and returns a new array upside down, changing the original array

sort: Sorting (positive sequence / reverse), no parameters / function that returns the new array after stitching, changing the original array

Digital sort without passing parameters, can only handle less than 10 real projects sort sort you need to pass parameters

var   ary = [20,5,45,100,89,3 ]; 

ary.sort ( function (A, B) { 

    return ab &; // ascending descending ba 
})

splice: CRUD array

 

Detailed splice CRUD
delete

Remove from the m index n, removed from the index n to the end

Delete some return to a new array change
New Remove from index n 0, x or more of the content into the front of the index n Delete some new array to return, if it is 0, it returns an empty array change
modify Remove from the m index n, x to remove content or more replacement Delete some return to a new array change

Demand expansion:

1 to remove the last of the array, there are several ways?

   1) pop()

   2) splice(ary.length-1)

   3) ary.length--

   4) delete ary [ary.length-1] is not recommended

2, additional new content to the end of the array, there are several ways?

   1) push(x,y,z)

   2)splice(ary.length,0,x)

   3) ary [ary.length]=x

slice: Find content in accordance with conditions

Method name effect parameter return value Whether to change the original array
slice Find content in accordance with conditions Find the index m from the index n, m is not included To find the content back to a new array constant
    0 array and may be implemented without parameters clone An array of shallow clone constant
    -3, -1 penultimate 3 penultimate one, does not contain a penultimate Find the contents of an array is returned constant

 

concat: mosaic array, the array to be spliced, a new array is returned after splicing, the same original array, may begin splicing, splicing parameters are arranged in order based on the empty array, the array does not occupy an empty position

toString: array to a string, no Returns string comma returned unchanged original array

join: the array into a string, a connector may be provided, the parameters are specified connection identifier and returns the string, the original array unchanged

Array summation:

Based on the + join using as a separator, each of the first array into a string added, based on the character string into expression eval execution js, the result is an accumulation of each of the array and

eval(ary.join('+'))

 indexOf / lastIndexOf: detecting a current position of the index values in the array first / last occurrence of the parameter value is to be detected, returns the index (-1 representing no), the same original array

Real project in order to verify whether an item contains

var ary = [2,5,6,84 ];
 IF (ary.indexOf (100)> -. 1 ) {
   // comprising ary 100 in this one 
}

These are just part of an array of methods, console input Array.prototype there will be many ways to say no detailed array, from shallow to deep, under an array of methods other then finishing it!

 

Guess you like

Origin www.cnblogs.com/xinxinxiangrong7/p/11424615.html