Turn-js operation of an array (incisive)

Source: Internet
Author: User

FROM:CSDN ligang2585116

The purpose of an array object is to use a separate variable name to store a series of values!
1. Shift: Deletes the first item of the original array and returns the value of the deleted element, returning undefined if the array is empty
2. Unshift: Add parameters to the beginning of the original array and return the length of the array, note: Their compatibility is poor
3. Pop: Delete the last item of the original array and return the value of the deleted element, or return undefined if the array is empty
4. Push: Adds a parameter to the end of the original array and returns the length of the array
5. Arrayobject.concat (Arrayx,arrayx,......, Arrayx): Returns a new array, which is the addition of parameters to the original array to form the new array
6. Arrayobject.splice (index,howmany,item1,....., ItemX): Add/Remove items to/from the array and return the deleted items. Delete the DeleteCount entry starting from the start position and insert the val1,val2 from that position,...
PS: All of the above methods can be replaced with splice or slice
Example one:

var a = [1,2,3,4,5];var B = a.shift (); Results: A for [2,3,4,5]b = 1var a = [1,2,3,4,5];var B = a.splice (0,1); Result: A is [2,3,4,5]b] [1]

Example two:

var a = [1,2,3,4,5];var B = A.unshift ( -2,-1); Results: A is [ -2,-1,1,2,3,4,5]b] 7var a = [1,2,3,4,5];a.splice (0,0,-2,-1); var b = a.length; Result: A is [ -2,-1,1,2,3,4,5]b = 7

Example three:

var a = [1,2,3,4,5];var B = A.pop (); Results: A for [1,2,3,4]b = 5var a = [1,2,3,4,5];var B = A.splice (a.length-1,1); Result: A is [1,2,3,4]b] [5]

Example four:

var a = [1,2,3,4,5];var B = A.push (6,7); Results: A is [1,2,3,4,5,6,7]b] 7var a = [1,2,3,4,5];a.splice (a.length,0,6,7); var B = a.length;//Result: A is [1,2,3,4,5,6,7]b 7]

Example five:

var a = [1,2,3,4,5];var B = A.concat (6,7); Result: A for [1,2,3,4,5]b = [1,2,3,4,5,6,7]var a = [1,2,3,4,5];var B = A; Point to the same address b.splice (b.length,2,6,7); Results: A for [[1,2,3,4,5,6,7]b] [1,2,3,4,5,6,7]//ps: Experiment five contact can be completely replaced with slice. <strong> Note the difference between the above experiment Five, b=a indicates that A and B point to the same address </strong>

7. Arrayobject.slice (Start,end): Returns the selected element from an existing array. Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.
Array Copy method one:

var array1 = new Array ("1", "2", "3"), var array2;array2 = Array1.concat (); array1.length = 0;alert (array2); Returns 1, 2, 3

Array Replication Method Two:

var array1 = new Array ("1", "2", "3"), var array2;array2 = array1.slice (0); array1.length = 0;alert (array2); Returns 1, 2, 3

Note that the splice () method is different from the slice () method, and the splice () method modifies the array directly, and the slice () method does not modify the arrays, but rather returns a subarray.
8. Empty the array:
Method One:

var ary = [1,2,3,4];ary.splice (0,ary.length); Console.log (ary); Output [], empty array, is emptied

Method Two:

var ary = [1,2,3,4];ary.length = 0;console.log (ary); Output [], empty array, is emptied

Method Three:

var ary = [1,2,3,4]; "The most efficient, recommended use" ary = []; Assign to an empty array to clear the original array

Method one or two retains the other properties of the array, and method three is not preserved.

9. Do not use the third variable exchange value:

Method One:

var a=2,b=3; A=[b, b=a][0]; Address point, must be an object

Method Two:

var a=2,b=3; A=b-a;b=b-a;a=b+a;

Method Three:

var a=2,b=3;a=a^b;b=a^b;a=a^b;
10. Find the maximum minimum value in the array
var arr = [1,2,3]console.log (Math.max.apply (Math,arr)); 3console.log (Math.min.apply (Math,arr)); 1

Reverse: Reverse the array

var a = [1,2,3,4,5];var B = a.reverse (); a:[5,4,3,2,1] b:[5,4,3,2,1]

Join (Separator): Sets the element group of the array as a string, separator as a delimiter, and the default comma delimiter if omitted

var a = [1,2,3,4,5];var B = A.join ("|"); a:[1,2,3,4,5] B: "1|2|3|4|5"

JS Sort
Sort (orderfunction): Sorts the array by the specified parameters

var a = [1,2,3,4,5];var B = a.sort (); a:[1,2,3,4,5] b:[1,2,3,4,5]
14. The array is an internal object provided by JavaScript, it is a standard collection, we can add (push), delete (shift) inside the element, we can also through the for loop inside of the elements, due to the language characteristics of JavaScript, We can dynamically add and remove properties to common objects.

Array:
NEW: var ary = new Array (); or var ary = [];
Added: Ary.push (value);
Deleted: Delete ary[n];
Traversal: for (var i=0; i < ary.length; ++i) ary[i];
Object:
NEW: var obj = new Object (); or var obj = {};
Added: Obj[key] = value; (Key is String)
Deleted: Delete Obj[key];
Traverse: for (var key in obj) Obj[key];

Code:

var keyword = '; for (var i=0; i<ary.length; ++i) {if (ary[i] = = keyword) {//Todo}}

Instead, we retrieve an entry for the specified key in object, just to use:
Code:

var key = '; var value = obj[key];//Todo

The complexity of the algorithm is divided into time complexity and space complexity. Time complexity refers to the computational effort required to execute the algorithm, while spatial complexity refers to the amount of memory space required to execute the algorithm.
This feature of object can be used to efficiently retrieve a unique set of strings, the time complexity of traversing an array is O (n), and the time complexity of object is O (1). Obviously, using object is more efficient!
Traversal efficiency (high to low) for a collection: var = obj[key]; > for (;;) > for (in). The least efficient is for (in), if the collection is too large, try not to use a for (in) traversal.

Turn-js operation of an array (incisive)

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.