JavaScript operations common methods for array objects

Source: Internet
Author: User
Tags prev

Conversion method

Because of the JavaScript internal mechanism (inheritance), all objects have tolocalstring (), toString (), ValueOf () methods, and array is no exception so:
var colors = ["Red", "Blue", "green"];
Alert (colors.tostring ()); Red,blue,green
Alert (colors.valueof ()); Red,blue,green
alert (colors); Red,blue,green
These methods of array inheritance return array items by default as a comma-delimited string, whereas the utility join () method can use a different delimiter to construct strings such as:
Alert (Colors.join ("|")); Red|blue|green

Stack method
Specifically, the array can behave like a stack, inserted in the stack called push-in, the stack to remove the call popup, so is actually push (), pop (),
The push method can accept any number of arguments, put them on one by one the end of the array, and return the modified array length, while the pop method removes the last item from the end of the array:
var colors = new Array (); Create an array
var count = Colors.push ("Red", "green"); Push in two items
alert (count); 2
Count = Colors.push ("Black"); Push into another item
alert (count); 3
var item = Colors.pop (); Get the last one
alert (item); "Black"
alert (colors.length); 2

Queue method
Push () is a way to add items to the end of an array, so you need only one method of getting items from the front of the array to simulate the queue. The array method for doing this is shift (), which moves the first item in the array and returns the item, minus 1 of the length. Using the shift () and push () methods together, you can use arrays as you would with queues.
var colors = new Array (); Create an array
var count = Colors.push ("Red", "green"); Push in two items
alert (count); 2
Count = Colors.push ("Black"); Push into another item
alert (count); 3
var item = Colors.shift (); Get the first item
alert (item); "Red"
alert (colors.length); 2
The ECMA also provides a way to Unshift (), which uses the opposite of the shift (), to add items to the front of the array, and to remove them from the end of the array.

Reorder Methods
There are two methods in the array that are directly used for sorting, namely reverse (), sort (), inverse arrays, and array sorting, and do not repeat them here.

Operation method
There are three main concat (), slice (), splice (),
The main purpose of concat is to create a new array based on all the items in the current array, or to combine arrays such as:
var colors = ["Red", "green", "blue"];
var colors2 = Colors.concat ("Yellow", ["Black", "Brown"]);
alert (COLORS2); Red,green,blue,yellow,black,brown

Slice creates a new array based on one or more items in the current array, you can accept one or two parameters, that is, to return the start and end position of the item (note: The end bit is not included, the method does not affect the original array) for example:
var colors = ["Red", "green", "blue", "yellow", "purple");
var colors2 = Colors.slice (1);
var colors3 = Colors.slice (1,4);
alert (COLORS2); Green,blue,yellow,purple
alert (COLORS3); Green,blue,yellow

The splice () method is the most powerful array method that can be deleted, inserted, and replaced with a set of items:
Splice (0,2) deletes the first two items in the array
Splice (2,0, "Red", "green") inserts the string "Red" and "green" starting at position 2 of the current array
Splice (2,1, "Red", "green") removes the entry for the current array position 2, and then inserts the string "Red" and "green" from position 2
The splice () method always returns an array that contains the items that were deleted from the original array (an empty array is returned if no items were deleted).

Location method
IndexOf () and LastIndexOf (). Both of these methods receive two parameters:
The subparagraphs (optional) to find indicates the index at which to find the starting point location. where the IndexOf () method starts looking backwards from the beginning of the array (position 0), the LastIndexOf () method starts looking forward from the end of the array, since the two methods are less than a description of the IE's poor support.

Iterative methods
The iterative method has every ()/filter ()/foreach ()/map ()/some () which are partially poorly compatible, with the main focus on ForEach and map:
Map (): Each item in an array runs the given function, and returns a list of the results of each function call, such as:
var numbers = [1,2,3,4,5,4,3,2,1];
var mapresult = Numbers.map (function (item, index, array) {
return item * 2;
});
alert (Mapresult); [2,4,6,8,10,8,6,4,2]

ForEach (): Runs the given function for each item in the array. This method has no return value. is essentially the same as the for-loop iteration Group:
var numbers = [1,2,3,4,5,4,3,2,1];
Numbers.foreach (function (item, index, array) {
Perform certain actions
});

Merge method
Two methods for merging arrays: reduce () and reduceright (). Both of these methods iterate over all the items of an algebraic group and then build a value that is ultimately returned. Both methods receive two parameters: a function that is called on each item and (optionally) the initial value that is the base of the merge.
You can use the reduce () method to perform an operation that evaluates the sum of all the values in the array, such as:
var values = [1,2,3,4,5];
var sum = values.reduce (function (prev, cur, index, array) {
return prev + cur;
});
alert (sum); 15

JavaScript operations common methods for array objects

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.