let log = console.log.bind(console);
let person = [
{id: 0, name: "小明"},
{id: 1, name: "小张"},
{id: 2, name: "小李"},
{id: 3, name: "小孙"},
{id: 1, name: "小周"},
{id: 2, name: "小陈"},
];

let obj = {};

person = person.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[]) //设置cur默认类型为数组,并且初始值为空的数组
log(person);

 

 

var oldArr = [
{id:1,name:"zhangs",age:18,contact_id:1},
{id:1,name:"zhangs",age:18,contact_id:2},
{id:1,name:"zhangs",age:18,contact_id:3},
{id:1,name:"zhangs",age:18,contact_id:14},
{id:1,name:"zhangs",age:18,contact_id:3},
{id:1,name:"zhangs",age:18,contact_id:2},
{id:1,name:"zhangs",age:18,contact_id:1}
];//原数据数组
var allArr = [];//新数组

1.该方法为jquery方法

$.each(oldArr,function(i,v){
var flag = true;
if(allArr.length > 0){
$.each(allArr,function(n,m){
if(allArr[n].contact_id == oldArr[i].contact_id){flag = false;};
});
};
if(flag){
allArr.push(oldArr[i]);
};
});
2.原生js方法

for(var i=0;i<oldArr.length;i++){
  var flag = true;
  for(var j=0;j<allArr.length;j++){
    if(oldArr[i].id == allArr[j].id){
      flag = false;
    };
  };
  if(flag){
    allArr.push(oldArr[i]);
  };
};

 

 

 

 

 

 

常用的

for (var i = 0; i < self.tableDataUser.length; i++) {
  if (self.radioUser.indexOf(self.tableDataUser[i].userId) > -1) {
    params = self.tableDataUser[i];
  }
}
return params;

 

posted on 2018-09-20 14:21  假装新手  阅读(161)  评论(0编辑  收藏  举报