Interpretation of Javascript Array method, javascript array

Source: Internet
Author: User

Interpretation of Javascript Array method, javascript array

Next to the previous article "basic introduction to Javascript Array", this article details all the methods of Array.

All Array methods are defined on Array. prototype, while Array. prototype is also an Array.

Array. concat ()

Copy the current array and append the received parameters to the end of the new array. The original array does not change.

Syntax

Array. concat (value1, value2,..., valueN)
The parameter is the array or non-array value to be merged.

var arr1 = [1, 2, 3];var obj = {animal : 'monkey'};var arr2 = arr1.concat([4, 5, 6], obj, [7, 8, 9]);// arr1 [1, 2, 3]// arr2 [1, 2, 3, 4, 5, 6, {animal : 'monkey'}, 7, 8, 9]obj.animal = 'tiger';// [1, 2, 3, 4, 5, 6, {animal : 'tiger'}, 7, 8, 9]

Array or non-array values can be merged. However, if an object is included, the object still references the original object.

Array. join ()

Returns a string that concatenates all elements of the array with delimiters. The default Delimiter is comma.

Syntax

Array. join (seperator)
The parameter is a delimiter.

var arr1 = [1, 2, 3];var str = arr1.join(); // 1,2,3str = arr1.join('#'); // 1#2#3

When a large number of string segments are assembled, the join method is faster than the + element operator.

The new Array (3) is used to generate an empty Array with a length of three, and the join () method can be used to repeat a string.

var str = new Array(3).join('-+'); // -+-+

The number of repetitions is the reduction of the array length by one, because the string is a separator.

Because the array itself is an object and has the toString () method, it can also be used to concatenate the array into a string, except that the separator can only be a comma.

var arr1 = [1, 2, 3];arr1.toString(); // 1,2,3

In fact, it will first call the toString () method of each element.

Array. push ()

Append one or more parameters to the end of the array and return the length of the array. Change the array itself.

Syntax

Array. push (value1, value2,..., valueN );
Instance

var arr1 = [1, 2, 3];var len = arr1.push(4, 5);console.log(len); // 5console.log(arr1); // [1, 2, 3, 4, 5]

Another way is to insert a value at the end of the array.

arr1[arr1.length] = 6; // [1, 2, 3, 4, 5, 6]  array.pop()

Delete the last entry of the array and return the deletion entry. Change the array itself.

Var arr1 = [1, 2, 3]; arr. pop (); // [1, 2] 3 is returned

If the array is empty, undefined is returned.

Array. unshift ()

Insert one or more parameters to the array header and return the array length. Change the array itself.

var arr1 = [1, 2, 3];var len = arr1.unshift(4, 5);console.log(len); // 5console.log(arr1); // [4, 5, 1, 2, 3]

Array. shift ()

Delete the first entry of the array and return the deletion entry. Change the array itself.

Var arr1 = [1, 2, 3]; arr. shift (); // [2, 3] returns 1

If the array is empty, undefined is returned.

Array. sort ()

This method sorts the values returned by the toString () method of each element, so the expected results are generally not obtained.

var arr1 = [1, 2, 3, 14, 24];arr1.sort(); // [1, 14, 2, 24, 3]

However, the sort () method can receive a UDF for comparison. The comparison function accepts two parameters. In particular, sort () is in ascending order by default. Therefore, if you want the first parameter to be placed before the second parameter, a negative number is returned. If the number is equal to 0, a positive number is returned after the first parameter is located.

var compare = function(a, b){  return a - b;}var arr2 = [1, 12, 2, 23, 3 , 5, 4];arr2.sort(compare); // [1, 2, 3, 4, 5, 12, 23]

You can use the string. localeCompare () method to compare strings.

var arr3 = ['F', 'e', 'f', 'E'];arr3.sort(function(a, b){  return a.localeCompare(b);});// ['e', 'E', 'f', 'F'] 

Array. reverse ()

Returns the array itself by reversing the order of array elements.

var arr1 = [1, 4, 3, 2];arr1.reverse(); // [2, 3, 4, 1]

Array. slice ()

Copy a portion of the array without changing the array itself.

Array. slice (start, end );
The method accepts two parameters. The last one can be omitted. The default value is the length of the array.

var arr1 = [1, 2, 3, 4, 5, 6];arr1.slice(4); // [5, 6]arr1.slice(2, 4); // [3, 4]arr1.slice(-3); // [4, 5, 6]

If a negative number is input, the length of the array is automatically added to try to become a non-negative number.
Input a value with an absolute value less than the length of the array, that is, an element that obtains the number of absolute values of negative numbers from the forward. For example, the last three elements are obtained.

Array. splice ()

This is the most powerful and commonly used method in the array. It can be deleted, inserted, or replaced.

Syntax

Array. slice (start, count, item );
This method removes one or more elements and replaces them with new elements. Start is the start position, count is the number of items to be deleted, and item is the newly added element (more than one item can be omitted). The deleted element is returned as an array.

Var arr1 = [1, 2, 3, 4, 5]; // Delete arr1.splice (2, 1); // [1, 2, 4, 5] return [3] // insert arr1.splice (3, 0, 6, 7); // [1, 2, 4, 6, 7, 5] // replace arr1.splice (1, 2, 8, 9); // [1, 8, 9, 6, 7, 5] returns [2, 4]

The following describes some new ECMAScript5 methods, which are not supported by ie8.

IndexOf () and lastIndexOf ()

Search for the index position of the corresponding item in the array. The second parameter indicates the starting position of the corresponding search direction. The first matching position is returned. If no matching position is found, the-1 is returned;
IndexOf () is a previous query, and lastIndexOf () is a later query.

var arr1 = [1, 2, 3, 4, 3, 2, 1];arr1.indexOf(2); // 1arr1.indexOf(2, 3); // 5arr1.lastIndexOf(3); // 4arr1.lastIndexOf(3, 4) // 2

Iteration Method

The following method accepts two parameters: the first is the scope of each function running and the second function running.
The running function has three parameters: current item, position, and array.

Array. every ()

Run the given function. If true is returned for every iteration, true is returned.

var arr1 = [1, 2, 3, 4, 5];arr1.every(function(item, index, array){  return item > 3;});// false

Array. some ()

Run the given function. If one of the iterations returns true, true is returned.

arr1.some(function(item, index, array){  return item > 3;});// true

Array. map ()

Run the given function and combine the values returned in the iteration into an array to return the array.

arr1.map(function(item, index, array){  return item * 2;});// [2, 4, 6, 8, 10]

Array. filter ()

Run the given function and return the elements that return true in the iteration as arrays.

arr1.filter(function(item, index, array){  return item > 3;});// [4, 5]

Array. forEach ()

Run the given function without returning any value. Similar to the common for loop function.

Merge Method

The function accepts two parameters. The first parameter is each running custom function, and the second parameter is the initial value used as the basis for merging.
The user-defined function accepts four parameters, namely the previous item, current item, position, and array.

Array. reduce () and array. export ceright () var splitstr = function (prev, item, index, array) {return prev + '#' + item;} var arr1 = [1, 2, 3, 4, 5]; arr1.reduce (splitstr, 8); // 8 #1 #2 #3 #4 #5arr1. reduceRight (splitstr, 8); // 8 #5 #4 #3 #2 #1

Summary

This article introduces the details and notes of the array method. The next article will introduce more advanced array usage. This article will introduce the newly added array method of ECMAScript6 in the future.

Articles you may be interested in:
  • Summary of some common methods for Array in js
  • How to transmit Array objects in JS to the background in JSON format
  • How to operate Array in JS and parse attribute instances
  • Js uses Array. prototype. sort () to sort 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.