Learning links: juejin.cn/post/684490…

Click on the link above for detailed articles

Double loop

function qc(arr)
{
    var i = 0;
    var j = 0;
    var tempArr = []
    for(i = 0; i<arr.length; i++){for(j = 0; j<tempArr.length; j++){if(arr[i]==arrrst[j]){
                break; }}if(j==tempArr.length){
            tempArr.push(arr[i])
        }
    }
    return tempArr
}
Copy the code

Indexof replaces the inner loop of a double loop

function qc2(arr)
{
    var i = 0;
    var tempArr = []
    for(i = 0; i<arr.length; i++){if(tempArr.indexOf(arr[i])<0){
            tempArr.push(arr[i])
        }
    }
    return tempArr
}
Copy the code

Splice deletes subsequent repetitions

Complicate the

function qc3(arr){
    var tempIndex = 0
    var tempArr = [...arr]// Create a clone
    for(var i = 0; i<tempArr.length; i++){ tempIndex = i+1
        while(tempIndex>=0){
            tempIndex = tempArr.indexOf(tempArr[i],tempIndex)
            if(tempIndex>=0){
                tempArr.splice(tempIndex,1)}}}return tempArr
}
Copy the code

The filter to filter

Indexof (ele) always returns the indexof the first ele queried, so index is not the first ele queried

function qc4(arr){
    return arr.filter(function(ele,index,self){
        return self.indexOf(ele) === index
    })
}
Copy the code

Sort to compare the current value with the last value

function qc5(arr){
    var tempArr = []
    var tempArr2 = [...arr]// Create a clone
    tempArr2 = tempArr2.sort()
    tempArr.push(tempArr2[0])
    for(var i = 1; i<tempArr2.length; i++){if(tempArr2[i-1]! =tempArr2[i]) { tempArr.push(tempArr2[i]) } }return tempArr
}
Copy the code

Take advantage of the uniqueness of object attributes

function qc6(arr){
    var tempObj = {}
    var tempArr = []
    for(var i = 0; i<arr.length; i++){if(tempObj[arr[i]]===undefined){
            tempObj[arr[i]] = "ok"
            tempArr.push(arr[i])
        }
    }
    return tempArr
}
Copy the code

Using Set sets does not repeat

function qc7(arr){
    return [...new Set(arr)]
    //return array. from(new Set(arr)
}
Copy the code