Several methods of native JS array de-weight

Source: Internet
Author: User
Tags new set

Sometimes when we do the project will often need to put some duplicate items in the array is removed, but the native JS has a sort, there are filters, and so on, but there is no array to how to do?
How can this be done by yourself. (The following code is directly added to the prototype method, for the same as the native JS method) can achieve the effect of array.xxx ()

The first method: Create an empty array using the IndexOf method to detect whether an item in the array is in a new array.

array.prototype.unique=function() {  var arr=[];   Create a new array   for(var i=0;i< this// traversal current array   if (Arr.indexof (This [i]===-1)) {// If it is equal to-1, then there is an item in the new array    Arr.push (This[i])}} , as in the current array. return arr;}
Call: Ary.unique ()

The second method: Create an empty array and an empty object to determine if the array is in the object

Array.prototype.unique=function(){varTmp={},arr=[] for(vari=0;i&lt; This. length;i++){if(!tmp[ This[i]]) {//if the TMP does not have this[i]tmp[ This[I]] =true;//depositedArr.push ( This[i]);//add to new array}}returnarr}//Call: Ary.unique ()

The third method: subscript Judgment method

 array.prototype.unique=function   () {  var  arr=[this  [0"];  for  (var  i=1;i<this . length;i++) {//  Iterate through the current array, starting with the second entry  if  (this . IndexOf (this  [i]===i)) {//  Arr.push (this  [i])}}  return   arr;}  //  call: Ary.unique ()  

Fourth method: After sorting in the go-to-weight

array.prototype.unique=function() {var arr=[];  This . Sort ()  for (var i=0;i<this. length;i++) {if(This [i]!==arr[arr.length-1 ] {arr.push (this[i])}}return  arr; // Call: Ary.unique ()

Fifth method: Black tech new set array de-weight


First, let's introduce the new Set

The new set is a collection of values from the Es6,set object, and you can iterate over its elements in the order in which they were inserted. The elements in the set appear only once, that is, the elements in the set are unique.

You can go see the MDN Document link address: Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set

Here's the code!

[... New Set ([1,1,2,2,3,3])]; // [A]

The code in this sentence, using the set element is unique, only one occurrence of the feature, there is a concept: "..."

... (extension operator)

Extension operators: Converting an array to a comma-delimited sequence of arguments

Since then it is very clear, black technology is not very magical, in fact, this is also a big factory interview problem, said is to use the least code to implement the array to heavy

If the newcomer is wrong, correct it, lest mislead others ~ humbly!

Several methods of native JS array de-weight

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.