數組{對象}去重 [ {},{},{}]

先看實例的原數組; 

//原數組對象
[
    {name: "NBA美國職業籃球聯賽", matchScreen: true},
    {name: "NBA美國職業籃球聯賽", matchScreen: true},
    {name: "美國職業大聯盟", matchScreen: true},
    {name: "阿根廷超級聯賽", matchScreen: true},
    {name: "墨西哥超級聯賽", matchScreen: true},
    {name: "哥倫比亞甲組聯賽", matchScreen: true},
    {name: "哥斯達黎加甲組聯賽", matchScreen: true}
]

 直接複製函數,調用把數組傳進去,當然你數組的對象裏面的屬性名不是name,你可以根據你需要做判斷去重的屬性名進去修改判斷。

unique(points){
        var newobj5=[];
        points.forEach(function(item,value,array){
				if(!newobj5[points[value].name]){   //此處 name 根據你數組對象裏面的屬性修改
					newobj5.push(points[value]);
					newobj5[points[value].name] = true;   //此處 name 同理修改
				}
        })
        return newobj5
    }

 最後你會得到你想要的不重複的數組

//去重後的數組
[
    {name: "NBA美國職業籃球聯賽", matchScreen: true},
    {name: "美國職業大聯盟", matchScreen: true},
    {name: "阿根廷超級聯賽", matchScreen: true},
    {name: "墨西哥超級聯賽", matchScreen: true},
    {name: "哥倫比亞甲組聯賽", matchScreen: true},
    {name: "哥斯達黎加甲組聯賽", matchScreen: true}
]

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章