Methods for array objects in JavaScript (detailed) _javascript tips

Source: Internet
Author: User
Tags arrays

JS Processing Array Multiple methods

The data types in JS are divided into two main categories: the original type and the object type.

The original type includes: numeric value, String, Boolean, null, undefined

Object types include: objects are collections of attributes, and of course there are two special objects----functions (a first-class object in JS), an array (an ordered set of key values).

Addition of array elements

Arrayobj.push ([Item1 [item2 [...] [Itemn]]]);

Adds one or more new elements to the end of the array and returns the new length of the array

Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]]);

Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned

Arrayobj.splice (insertpos,0,[item1[, item2[, ...) [, Itemn]]]);

Inserts one or more new elements into the array at the specified position, the elements of the insertion position are automatically moved back, and the

Deletion of array elements

Arrayobj.pop ();

Removes the last element and returns the element value

Arrayobj.shift ();

Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Arrayobj.splice (Deletepos,deletecount);

Deletes the specified number of DeleteCount elements starting at the specified position, deletepos the removed elements

Interception and merging

Arrayobj.slice (start, [end]);

Returns a portion of an array as an array, noting that the end-corresponding element is not included, and if omitting the end copies all elements after start

Arrayobj.concat ([item1[, item2[, ...) [, Itemn]]]);

Concatenate multiple arrays (or a string, or a mixture of arrays and strings) into an array, returning a new array of connections

Copy of array

Arrayobj.slice (0);

Returns an array of copies, noting that a new array is not a pointer to the

Arrayobj.concat ();

Returns an array of copies, noting that a new array is not a pointer to the

Ordering of array elements

Arrayobj.reverse ();

Reverse element (top to last, last to top), return array address

Arrayobj.sort ();

array element Sorting, returning the arrays address

Insertion of array elements

Arrayobj.splice (insertpos,0, [item1[, item2[, ...) [, Itemn]]]);

Inserts the specified item element from the Insertpos position, and 0 represents the deletion of 0 elements, returning an empty array

Substitution of array elements

Arrayobj.splice (Insertpos,replacecount, [item1[, item2[, ...) [, Itemn]]]);

Deletes the Replacecount element from the Insertpos location, adds the specified item element from the Insertpos position, and returns the deleted element as an array

The position of the array element

Arrayobj.indexof (Findthing,start);

From the starting point you want to find (optional) start to look backwards to find the item findthing, find the criterion is congruent, find the place to return the value, can not find the return-1

Arrayobj.lastindexof (Findthing,number)

Start with the starting point you want to find (optional) begin to find the item findthing, find the criterion is congruent, find the place to return the value, can not find the return-1

Iteration of an array element

Arrayobj.every ()

Runs the given function for each item in the array, and returns True if the function returns true for each item

Arrayobj.filter ()

Runs the given function for each item in the array, returning an array of items that the function returns True

Arrayobj.foreach ()

Run the given function for each item in the array, this method has no return value

Arrayobj.map ()

Each item in the array runs the given function, returning an array of the results of each function call

Arrayobj.some ()

Runs the given function for each item in the array, and returns True if the function returns true for either item

Merging of array elements

Arrayobj.reduce (prev, cur, index, array)

Starting with the first item of the array, conveniently to the last, four parameters are the previous value, the current value, the index and array of items, and any values returned by the function are automatically passed to the next item as the first argument

000

Arrayobj.reduceright ()

From the end of the array, one convenient to the first, four parameters are the previous value, the current value, the index and array of items, and any values returned by the function are automatically passed to the next item as the first argument

string of array elements

Arrayobj.join (separator);

Returns a string that connects each element value of an array, separated by a separator in the middle.

toLocaleString, toString, valueof: Can be seen as a special use of join, not commonly used

Tosource () returns the source code for this object

ToString () converts the array to a string and returns the result

toLocaleString () converts the array to a local array and returns the result

ValueOf () returns the original value of an array object

ES5 New

Array.prototype.indexOf

The IndexOf () method returns the position of the first element found in the array, or 1 if it does not exist.

Array.prototype.lastIndexOf

Array.prototype.every

Array.prototype.some

Array.prototype.forEach

foreach executes the corresponding method for each element

Array.prototype.map

Map () After each element of the array is manipulated (mapped), a new one is returned

Map () is a very useful function when processing server returns data

Array.prototype.filter

The filter () method creates an array of new matching filter criteria.

Array.prototype.reduce

Reduce () can implement an accumulator function that lowers each value of the array (left to right) to a value

Scenario: Counting how many words in an array are not repeated

Array.prototype.reduceRight

The above JavaScript about the Array object method (detailed) is the small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.