js 去掉数组对象中的重复对象
程序员文章站
2022-03-20 15:36:45
调用- let obj = [{"name": "1","url": "1u"}, {"name": "1","url": "1r"}, {"name": "2","url": "1u"},{ "name": "1", "url": "1u" }]; con...
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"}]