Operation of an array Array.prototype.sort method--alert ([1, 3, 9, 2].sort ())

Source: Internet
Author: User
Tags array sort

1. The array sort method is far from being fully exploited and may be more powerful than the developers think. Many developers may feel that the sort method can be used to do this kind of thing:

    [1, 3, 9, 2].sort ();    // return [1, 2, 3, 9]    alert ([1, 3, 9, 2].sort ())

...... That's true, but it has more powerful uses, like this:

[{name: "Robin Van pursestrings", age:30}, {name: "Theo Walcott", age:24}, {name: "Bacary Sagna", age:28}].sort (function(Obj1, Obj2) {//Ascending:first age less than the previous// implements an ascending order: The former has an age less than the latter  Obj2.age;}); // Returns: // [//// {name: "Bacary Sagna", age:28}, // {name: "Robin Van pursestrings", age:30} // 

   var oldarr=[1,4,9,2,8,3,78,68];  
   alert (function (A, b) {return A-B})
You can sort objects by properties, not only for simple types of array items. If one day the server sends a piece of JSON data, and the objects in it need to be sorted, don't forget this trick!



Using push to merge arrays

The array of slice and apply methods can be combined with a few small coup, so for the array method of other techniques, you should be ready to prepare for it. This time use the push method to merge the array:

    var mergeto = [4,5,6];      var mergefrom = [7,8,9];            Array.prototype.push.apply (mergeto, mergefrom);            //

This is an obscure trick, and simple native methods can be used to implement common tasks such as array merging. This method is not only clever except that the push method can receive multiple parameters, but also involves the use of the second parameter of the Apply method. )


Push (args) can press multiple elements at a time and return the updated array length. The pop () function only pops the last element at the end and returns the element that pops up, and if it is called pop () for the number of empty groups, it returns undefined

var oldarr=[1,2,3];
Alert (Oldarr.push (4,[5,6])) –>5 (this only evaluates [5,6] as an element, returning the updated array length 5)
At this time oldarr–>[1,2,3,4,[5,6]]
Alert (Oldarr.pop ()) –>[5,6] (this pops up the last element [5,6] instead of 6)
At this time oldarr–>[1,2,3,4]
Oldarr.pop () –>4
Oldarr.pop () –>3
Oldarr.pop () –>2
Oldarr.pop () –>1
Oldarr.pop () –>undefined (empty array popup)

Start in Splice (Start,deletecnt,args) indicates the beginning of the subscript, deletecnt indicates the number of elements to be removed from the start of the subscript, including the element, and the delete operation returns the deleted element.
Args is used to replace deleted elements (which can have multiple arguments), and start and deletecnt must be numbers, and if not the number is attempted, the conversion fails as the zero processing.

var oldarr3=[1,2];
Oldarr3.splice () –> "" (Returns an empty string, without any action, after Operation oldarr3–>[1,2])
Oldarr3.splice ("") –>[1,2] ("" Attempt to convert to a number failed to return 0, so delete 1, 2, after Operation Oldarr3–>[], but IE is a bit disgusting, do not do any action)
Oldarr3.splice ("1a") –> ibid.
Odlarr3.splice (0,2) –>[1,2] ("Start with the elements of subscript 0, delete two elements so delete oldarr3–>[])
Oldarr3.splice (0,-1) –> "" (starting from 0 subscript Delete-1 elements, so equal to no action, after Operation oldarr3–>[1,2])
Oldarr3.splice (–>2) (from subscript 1 to delete 1 elements, that is, delete 2, so after deletion oldarr3–>[1])
Oldarr3.splice (1,4) –>2 (delete 4 elements starting from subscript 1, 1 starts with only 1 elements, so delete 2, so delete oldarr3–>[1])
Oldarr3.splice ( -1,0,3) –> "" (remove 0 elements starting from subscript-1 i.e. 2 elements, then add element 3, so after Operation oldarr3–>[1,3,2])
Oldarr3.splice ( -1,1,3) –>2 (starting from 1, 2 elements to delete 1 elements and then adding element 3, after operation is oldarr3–>[1,3])


Concat, this method is used to concatenate two or more arrays, and the array does not change the original array but only returns a new array. When the parameter is concatenated, the element in the array is concatenated.

var oldarr4=[1,2];
Oldarr4.concat (3,4) –>[1,2,3,4]
Oldarr4.concat (3,4,[5,6]) –>[1,2,3,4,5,6] (this side adds element 5 and element 6 in [5,6])
Oldarr4.concat (3,[4,[5,6]]) –>[1,2,3,4,[5,6] [the innermost element of this side [5,6] is used throughout to add, not disassemble)

Operation of an array Array.prototype.sort method--alert ([1, 3, 9, 2].sort ())

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.