Deduplication of object arrays based on a certain field - one of the methods

Look at the code:

var arr=[
        {
            id:1,name:'aa',age:10
        },
        {
            id:2,name:'bb',age:10
        },
        {
            id:3,name:'cc',age:20
        },
        {
            id:4,name:'aa',age:20
        },
    ]

    // 去重
    let zz = []
    arr.forEach(r=>{
        if(!zz.some(rr=>rr.name===r.name)){
            zz.push(r)
        }
    })
    console.log('zz ',zz);

After printing:

 

Guess you like

Origin blog.csdn.net/weixin_42220533/article/details/127542185