El siguiente código devuelve una tabla con valores de una lista web. Algunos valores son duplicados. Necesito eliminar todos los duplicados donde "User_x0020_Name", "Previous_Total_Most_Likely_Forec", "Submitted_Total_Most_Likely_Fore" son iguales y solo mantengo el registro más reciente (máximo de "Creado") para un conjunto de duplicados.
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")} }); }No he verificado esto, pero tal vez puedas hacer algo como esto:
Usar un mapa hash para almacenar la ubicación del índice del triplete único para reemplazarlo cuando encontremos uno más nuevo.
// This will be our final result array, with no duplicates. const result = [] // Here we will store the result index location of a stored item. const latest_map = new Map() // build a unique key for the triplet combo. function buildKey(item) { const separator = "__" return item.User_x0020_Name + separator + item.Previous_Total_Most_Likely_Forec + separator + item.Submitted_Total_Most_Likely_Fore } // check if item exist, replace function deduplicateAndKeepLatest(item) { const key = buildKey(item); if (latest_map.has(key)) { result[latest_map.get(key)] = item; // replace old item with latest. } else { result.push(item); latest_map.set(key, result.length-1) // first time seeing this item, save the index it was stored at. } } // make sure items is in the correct sort order, since you want to keep the latest. items.forEach((item) => { deduplicateAndKeepLatest(item); } return result; // should contain unique triplet, and only latest one.