数组去重

2018-04-28  本文已影响0人  duansiyu

普通数组去重

引入underscore.js 

_.uniq([1, 2, 1, 3, 1, 4]);

=> [1, 2, 3, 4]

json数组

obj2key(obj, keys) {

var n = keys.length,

    key = [];

  while (n--) {

key.push(obj[keys[n]]);

  }

return key.join('|');

}

uniqeByKeys(array, keys) {

var arr = [];

  var hash = {};

  for (var i =0, j = array.length; i < j; i++) {

var k =this.obj2key(array[i], keys);

    if (!(kin hash)) {

hash[k] =true;

      arr.push(array[i]);

    }

}

return arr;

}

var arr = [{rid:1,name:'李白'},{rid:2,name:'张三'},{rid:1,name:'赵云'},{rid:1,name:'李四'}]

this.uniqeByKeys(arr, ['rid']);

上一篇下一篇

猜你喜欢

热点阅读