1字符串数组去重
var arr1 = ['xue','miao','xue','song','song','代码'];
var arr2 = [];
for(var i=0;i<arr1.length;i++){
console.log(arr1.length)
if(arr2.indexOf(arr1[i])<0){
arr2.push(arr1[i])
}
}
console.log(arr2)
2对象数组id一致就显示新的对象,去除旧对象
var phoneList = [
{ phone: 1, msg: “a” },
{ phone: 2, msg: “b” },
{ phone: 3, msg: “c” }
];
var newphone = { phone: 2, msg: “b”, sex: “female” };
for (let i = 0, len = phoneList .length; i < len; i++) {
if (phoneList [i].phone=== newphone.phone) {
phoneList[i] = newphone ;
}
}
console.log(phoneList );
3严格对象数组去重
var list = [
{ id: 1, name: “a” },
{ id: 2, name: “b” },
{ id: 3, name: “c” }
];
var replacement = { id: 2, name: “b”, sex: “female” };
for (let i = 0, len = list.length; i < len; i++) {
if (list[i].id === replacement.id&list[name]===replacement.name) {
list[i] = replacement;
}
}
console.log(list);