调用:
let results = deleteObject(obj);
console.log(results);
方法:
function deleteObject (obj) {
var uniques = [];
var stringify = {};
for (var i = 0; i < obj.length; i++) {
var str = JSON.stringify(obj[i]['name']);
if (!stringify.hasOwnProperty(str)) {
uniques.push(obj[i]);
stringify[str] = true;
}
}
uniques = uniques;
return uniques;
}
es6写法:
export const deleteObject = (obj) => {
var uniques = [];
var stringify = {};
for (var i = 0; i < obj.length; i++) {
var str = JSON.stringify(obj[i]['name']);
if (!stringify.hasOwnProperty(str)) {
uniques.push(obj[i]);
stringify[str] = true;
}
}
uniques = uniques;
return uniques;
};
或
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; }