导航

JavaScript list 去重复

Posted on 2016-08-17 10:41  _eve  阅读(891)  评论(0编辑  收藏  举报
function unique(arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
        if (!hash[elem]) {
            result.push(elem);
            hash[elem] = true;
        }
    }
    return result;
}

function unique(arr, attribute){
  var result = [], hash = {};
  for (var i=0; i<arr.length; i++) {
  var elem = arr[i][attribute];
  if (!hash[elem]) {
    result.push(arr[i]);
    hash[elem] = true;
  }
}
  return result;
}