Several ways to de-weigh javascript arrays

Source: Internet
Author: User

Several ways to de-weigh javascript arrays
  1. Creates a new empty array, iterates through For...of (ES6), and, by IndexOf, determines whether the element exists in the new array, and then push the nonexistent (INDEXOF (n) ==-1) element to the new array:

      let arr   = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2];    function Removedup_indexof (Originalarr) {Let newArr = [];        for (n of Originalarr) {if (Newarr.indexof (n) ==-1) {Newarr.push (n);   }} return NEWARR; } Let arr1 = Removedup_indexof (arr);  
    The

    can also first place the first item of the original array in a new array, starting with the second entry to determine the loop:

      Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2]; function Removedup_index (Originalarr) {//index=    0 of the items are added to the array first, then from the second start loop let NEWARR = [originalarr[0]]; for (Var i=1;i<originalarr.length;i++) {if (Originalarr.indexof (Originalarr[i]) ==i) {Newarr.push (origin        Alarr[i]);   }} return NEWARR; } Let arr2 = Removedup_index (arr);  
  2. The
  3. uses the comparison of adjacent arrays to push the elements that are different from the previous element into the array, NOTE : This way, the array is sorted first, and the original array is sorted deep with the slice () method to avoid affecting the original array.

      Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2];function compareadjoin (Originalarr) {let ordered       ARR = Originalarr.slice (). sort ();//sorted first, then adjacent array elements are compared let NEWARR = [orderedarr[0]]; for (Var i=1;i<orderedarr.length;i++) {if (orderedarr[i]!=orderedarr[i-1]) {Newarr.push (ordered            Arr[i]);   }} return NEWARR; } Let Arr3 = Compareadjoin (arr);  
  4. Takes advantage of Object{key:value}, which is an attribute that cannot be duplicated by a key value. Create a new obj{}, add each element of the array as a key to obj, and push the element that does not exist obj[arr[i]] to the new array, obj= {arr[1]: Value,arr[2]:value ...} Note: There is no need to pay attention to value values, can be arbitrarily copied, only by key to determine whether the element has been added.

      Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2], function removedup_obj (Originalarr) {Let Newa        Rr=[];        Let obj={};                for (n of Originalarr) {if (!obj[n]) {obj[n] = 1;            Newarr.push (n);   }} return NEWARR; } Let ARR4 = Removedup_obj (arr);  
  5. The flashback loop (to avoid the change in the length of the array in the deletion process) ordered array, will be the same as the next element through the splice method removed from the original array, with the same #, the array first sorted, with the slice () method deep copy the original array to order, to avoid affecting the original array.

     let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2]; function removeDup_splice(originalArr){       let orderedArr = originalArr.slice().sort();       for(var i = orderedArr.length-1;i>0;i--){            if(orderedArr[i-1]===orderedArr[i]){                orderedArr.splice([i-1],1);            }       }       return orderedArr;   }   let arr5 = removeDup_splice(arr);

Several ways to de-weigh javascript arrays

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.