I have a small requirement where in i need to remove the duplicate values from the array that i am getting.
Overview
In the below code, what i am trying to do is to get top 1000 employee names(Should Cost Modeler People Picker Field) through REST API from List A and appending those values in LIST B Drop down. But as i get all the names through REST API there are many duplicate values in the array.
What i would like to do is to remove the duplicate values and then append the values to drop down.
//Get the employee names
function getAllEmployeeNames(){
var results;
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('Should Cost Projects')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id&$top=500",
type: "GET",
async:false,
headers: {
"Accept": "application/json;odata=verbose",
},
success: function (data) {
if(data.d.results.length>0){
results=data.d.results;
}
},
error: function (data) {
//alert("Error");
}
});
return results;
}
// To Append the Employee Name to drop down on form load.
var employeeName=$("input[title^='Employee Name']").val();
$("input[title^='Employee Name']").hide();
$("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
$("input[title^='Project Name']").hide();
$("input[title^='Project Name']").after("<select id='ProjectField' class='ms-RadioText'><option value=''></option></select>");
var allEmployeeNames=getAllEmployeeNames();
console.log(allEmployeeNames);
$.each(allEmployeeNames,function(i,employee){
$.each(employee.Should_x0020_Cost_x0020_Modeler.results,function(j,item){
if(employeeName==item.Title){
$("#EmployeeField").append("<option selected='selected' value='"+item.Title+"'>"+item.Title+"</option>");
}else{
if(item.Title!=undefined){
$("#EmployeeField").append("<option value='"+item.Title+"'>"+item.Title+"</option>");
}
}
});
});
// the code that i am using to remove duplicates but it is not working.
$(allEmployeeNames).each(function() {
$(this).siblings('[value="'+ $(this).val() +'"]').remove();
});
Please help me solve this problem.
Thanks
$topas query option. stackoverflow.com/questions/25744763/…