JavaScript Array Common Functions Summary

Source: Internet
Author: User
Tags javascript array

[20141121] JavaScript Array Common Features summary

Introduction : in JavaScript, array is an object that is used more frequently, so what are the usual methods?

First, let's look at the type of the array object:

typeof Array // ‘function‘Array instanceof Object // true

As can be seen from the above, the array essence is a function, also derived from object, defined as follows:

function Array(args) {}
Next, let's look at the method of the array itself: #1, concat ()

Definition: A prototype method that joins two or more arrays and returns a result (a new array).

Array.prototype.concat = function(items) {};

Example:

var arr1 = [1, 2];var arr2 = arr1.concat([3, 4]);var arr3 = arr2.concat([5, 6], [7, 8] ,10, {});console.log(arr1); // [1, 2]console.log(arr2); // [1, 2, 3, 4]console.log(arr3); // [1, 2, 3, 4, 5, 6, 7, 8,  10, Object]

Note:concat can connect not only individual objects, but also multiple objects, and if the parameters are arrays, the array elements are split and concatenated, and if they are objects, the objects are connected directly. The method does not change the original array

#2, join ()

Definition: A prototype method that places all elements of an array into a string. element is delimited by the specified delimiter.

Array.prototype.join = function(separator) {};

Example:

var arr = [1, 2, 3];console.log(arr.join(‘|‘)); // ‘1|2|3‘console.log(arr.join(‘‘));  // ‘123‘console.log(arr.join(‘---‘));  // ‘1---2---3‘

Note: too often, there is nothing to notice ~

#3, Pop ()

Definition: The prototype method, which deletes and returns the last element of the array.

Array.prototype.pop = function() {};

Example:

var arr1 = [1, 2, 3, 4];var lastOne = arr1.pop();console.log(lastOne);  // 4console.log(arr1);     // [1, 2, 3]

Note: the method has no arguments, returns a return value, and returns the last element of the array. The method will change the original array

#4, push ()

Definition: A prototype method that adds one or more elements to the end of an array and returns a new length.

Array.prototype.push = function(items) {};

Example:

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

Note: The return value of this method returns the new length of the array. The method will change the original array

#5, Reverse ()

Definition: A prototype method that reverses the order of elements in an array.

Array.prototype.reverse = function() {};

Example:

var arr1 = [1, 2, 3, 4, 5];var res = arr1.reverse();console.log(res);console.log(arr1);

Note: The return value of the method is itself (the inverted value), and the method changes the original array

6, Shift ()

Definition: A prototype method that deletes and returns the first element of an array.

Array.prototype.shift = function() {};

Example:

var arr1 = [1, 2, 3];var res = arr1.shift();console.log(res);console.log(arr1);

Note: The method returns the first element of the array, corresponding to the Pop () method (returns and deletes the last element). The method will change the original array

#7, Slice ()

Definition: A prototype method that returns the selected element from an existing array.

Array.prototype.slice = function(start,end) {};

Example:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];var res1 = arr.slice(0, 3);var res2 = arr.slice(0, 100);var res3 = arr.slice(-1,-6);var res4 = arr.slice(-6, -1);console.log(res1);console.log(res2);console.log(res3);console.log(res4);console.log(arr)

Note: This method supports reverse indexing, while the index adopts the principle of interval left closed right opening. The method does not change the original array

#8, sort ()

Definition: A prototype method that sorts elements of an array.

Array.prototype.sort = function(compareFn) {};

Example:

var arr = [1, 5, 2, 3, 4, 7, 8, 6, 9];var res1 = arr.sort(); //如果是数字,默认从小到大排序console.log(res1);var arr2 = [‘a‘, ‘c‘, ‘b‘];var res2 = arr2.sort();//如果是字符,按照字符顺序(ASCII,字符串同)排序console.log(res2);//遇到复杂数据,经过测试是按照数组<正则<数字<对象<字符串<函数 这个顺序var arr3 = [{name:‘name‘}, 134, ‘aaa‘, function(){}, [], /a/];var res3 = arr3.sort();console.log(arr3);//可以通过自定义规则实现复杂的排序var res4 = arr.sort(function(a1, a2){    if(a1 === a2){ // 两者相等,那么就算想等        return 0;    }    if(a1%3 === 0){ //如果a1被3整除,那么a1小        return -1;    }    if(a2%3 === 0){ //如果a2被3整除,那么a2小        return 1;    }    return a2%3-a2%3; //不满足以上条件,那么根据余数比大小,余数小的元素小})console.log(res4);

Note: The method returns itself (sorted array). A very complex collation can be implemented through function (A1, a2) {}. The method will change the original array

#9, Splice ()

Definition: Prototype method, delete element, and add new element to array. (The method is more complex and easy to use)

Array.prototype.splice = function(start,deleteCount,items) {};

Example:

var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];var res1 = arr1.splice(0, 3, ‘new1‘, ‘new2‘);console.log(res1);  // [1, 2, 3] console.log(arr1);  // [‘new1‘, ‘new2‘, 4, 5, 6, 7, 8, 9] arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];res1 = arr1.splice(-6, 3, ‘new1‘, ‘new2‘);console.log(res1);  // [4, 5, 6]console.log(arr1);  // [1, 2, 3, ‘new1‘, ‘new2‘, 7, 8, 9]

Note:the splice () function supports a flashback index, while the second parameter is length (not subscript), and the newly inserted data is inserted at the start subscript position. The return value is an array of elements that are deleted. The method will change the original array

#10, Unshift ()

Definition: A prototype method that adds one or more elements to the beginning of an array and returns a new length.

Array.prototype.unshift = function(items) {};

Example:

Note: This method is relative to push (adds an element at the end, returns a new length), and the return value of the method is the new array length. The method will change the original array

We can also add more common features to the array, such as:
Array.prototype.where = function(predicateFn){    var parameterIsFn = typeof predicateFn === ‘function‘    var result = [];    for(var i = 0, len = this.length; i < len; i++){        if(!parameterIsFn || predicateFn(this[i])){            result.push(this[i]);        }    }    return result;};var arr = [‘new1‘, ‘new2‘, 1, 2, 3];var res = arr.where(function(item){    return typeof item === ‘number‘;});console.log(res);

JavaScript Array Common Functions Summary

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.