Talking about deduplication of complex array data

Today, during the development process, we implemented the function of linking the top menu and the bottom menu, and found that when the top menu was clicked repeatedly, the interface view menu was repeatedly displayed, so I thought of deduplicating the array to solve this bug.

At present, the most concise way of writing is to use ES6 set to deduplicate

let tabList=[{"name":"系统监控","path":"/monitor"},{"name":"银行信息","path":"/bankinfo"},{"name":"银行信息","path":"/bankinfo"}]

function unique(arr){
    return Array.from(new Set(arr))    //或者写成 return [...new Set(arr)]
}

console.log(unique(tabList))

2. With the help of double for loops, compare the unique attributes to deduplicate

let tabList=[{"name":"系统监控","path":"/monitor"},{"name":"银行信息","path":"/bankinfo"},{"name":"银行信息","path":"/bankinfo"}]
function unique(arr){
    for(let i=0;i<arr.length;i++){
        for(let j=i+1;j<arr.length;j++){
            if(arr[i].name===arr[j].name){
                arr.splice(j,1) //删除后面重复元素
            }
        }
    }
    return arr
}
console.log(unique(tabList))

Guess you like

Origin blog.csdn.net/qq_30596783/article/details/120313788