乐趣区

关于javascript:js-数组去掉重复的值返回一个新数组

比方
let arr=[{type:1,num:1},{type:2,num:2},{type:1,num:1}]
// 去掉反复值
export function deteleObject(obj) {
var uniques = [];
var stringify = {};
for (var i = 0; i < obj.length; i++) {

  var keys = Object.keys(obj[i]);
  keys.sort(function(a, b) {return (Number(a) - Number(b));
  });
  var str = '';
  for (var j = 0; j < keys.length; j++) {str += JSON.stringify(keys[j]);
      str += JSON.stringify(obj[i][keys[j]]);
  }
  if (!stringify.hasOwnProperty(str)) {uniques.push(obj[i]);
      stringify[str] = true;
  }

}
uniques = uniques;
return uniques;
}
返回一个新数组大略就是这个成果
arr=[{type:1,num:1},{type:2,num:2}]

退出移动版