The below code returns a table with values from a web list. Some values are duplicates. I need to remove all duplicates where "User_x0020_Name", "Previous_Total_Most_Likely_Forec", "Submitted_Total_Most_Likely_Fore" are the same and only keep the latest record (maximum of "Created") for a duplicates set.
function loadAuditTrailFinancials() {
var auditTrailURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Audit_Trail_Financial_Data')/items?$select=Author/Title,Previous_Total_Most_Likely_Forec,Submitted_Total_Most_Likely_Fore,Forecast_Approval_Reason,Title,Created,Workflow_Stage_Name,WorkflowStageShort,Source,Event&$filter=Title eq '" + PDP_projUid + "'&$orderby=Created desc&$expand=Author/ID";
console.log("loadAuditTrailFinancials:" + auditTrailURL);
$.ajax({
url: auditTrailURL,
method: "GET",
headers: {"Accept": "application/json; odata=verbose"},
success: function (data) {
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var creation_date = items[i].Created;
var current_user = items[i].User_x0020_Name;
console.log(items[i]);
$('#AuditTrailTable_Financial').append('<tr class="audit_content">' +
'<td align=center> ' + format_date(creation_date) + ' </td>' +
'<td align=center> ' + items[i].WorkflowStageShort+ ' </td>' +
'<td align=center> ' + items[i].Author.Title + ' </td>' +
'<td align=center> ' + items[i].Source + ' </td>' +
'<td align=center> ' + items[i].Previous_Total_Most_Likely_Forec + ' </td>' +
'<td align=center> ' + items[i].Submitted_Total_Most_Likely_Fore + ' </td>' +
'</tr>');
}
$('.audit_content').hide();
console.log(data);
},
error: function (data) { alert("Some error occurred in getting Audit Trail")}
});
}
3
Answers
Thank you everyone for your replies.
I have sorted the table in descending order and used the below code to solve the issue:
Maybe you can do this using a hash map to store index location of the unique triplet in order to replace it when we find a newer one.
reference:
Array.prototype.filter()
Array.prototype.findIndex()