Javscript using object attributes to remove array duplicates and sort

Source: Internet
Author: User
Tags hasownproperty

when there is an array of a=[1,2,3,4], there is an object A={0:1,1:2,2:3,3:4}, and then run alert (A[0]), both cases run the result is 1, that is to say, the data collection can be expressed as an array, or can be represented by an Object! But We can't define an object with two identical key values, and we can use this feature to remove duplicates from the array.

    • Turn the array into a JS object
    • Change the value in the array to the key in the object
    • To restore an object to an array
// turn the array into a JS object
functionToobj (arr) {varobj = {};  for(vari=0;i<arr.length;i++) {Obj[arr[i]]=true; }    returnobj;}functionObjkeys (obj) {vararr = [];  for(varattrinchobj) {        if(Obj.hasownproperty (attr)) {//use hasOwnProperty more rigorousArr.push (attr); }    }    returnarr;}functionUniq (arr) {returnObjkeys (Toobj (arr));}varARR1 = [4,5,2,56,56,54,4,4,2,9,10]alert (Uniqsort (arr1)); //2,4,5,9,10,54,56

We found that this method can not only give weight and can also be sorted, double benefit!

The above mentioned hasOwnProperty method illustrates the following:

The hasOwnProperty function method in JavaScript is to return a Boolean value that indicates whether an object has a property of the specified name. How to use:
Object.hasownproperty (Proname)
Where the Parameter object is a required option. An instance of an object.
Proname is a mandatory option. A string value for the name of a property.

If object has a property of the specified name, then the hasOwnProperty function method in JavaScript returns True, and vice versa. This method cannot check whether the object has this property in its prototype chain, and the property must be a member of the object itself. In the following example, all the String objects share a common split method. The following code will output false and true.
var s = new String ("JScript");

Print (S.hasownproperty ("split"));

Print (String.prototype.hasOwnProperty ("split"));

Another way of thinking about the other methods we used

1. Build a new array to store the results

Each time an element is removed from the original array in a 2.for loop, the loop is compared with the result array.

3. If the element is not in the result array, it is stored in the result array

-------------------

1. Sort the original array first

2. Check that the first element in the original array is the same as the last element in the result array, because it is already sorted, so the repeating element is in the adjacent position

3. If it is not the same, the element is stored in the result array

-------------------

1. Create a new array to hold the result

2. Create an empty object

3.for Loop, each time an element is taken out and compared to the object, if the element is not duplicated, it is stored in the result array, and the content of the element as an object property, and assigned a value of 1, deposited into the object set in the 2nd step

    • The above method code does not show, Baidu has, the second method has the limitation is that it broke the array of the original sort!

Javscript using object attributes to remove array duplicates and 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.