js 去掉数组对象中的重复对象

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;
}

调用-

 

let obj = [{
"name": "1",
"url": "1u"
}, {
"name": "1",
"url": "1r"
}, {
"name": "2",
"url": "1u"
},{

"name": "1",

"url": "1u"

}];

console.log(deteleObject(obj))   // [{"name": "1","url": "1u"},{"name": "1","url": "1r"},{"name": "2","url": "1u"}]

 

上一篇:Spring练习,使用Properties类型注入方式,注入MySQL数据库连接的基本信息,然后使用JDBC方式连接数据库,模拟执行业务代码后释放资源,最后在控制台输出打印结果。


下一篇:遮挡mask