在数组对象中去掉重复的对象:
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; }
eg:
总结: 亲测好使,封装一个这样的函数是不是很好用呢,
原文地址:https://www.cnblogs.com/gaoht/p/9850449.html
时间: 2024-11-10 01:20:16