0

I have an ajax code as below :

<script>
  $(document).ready(function () {
  EmployeeStates("Available", 1, 15);
});
function EmployeeStates(state, pageNumber, pageSize) {
  $.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost:58102/api/EmployeeState?state=" + state + "&pageNumber=" + pageNumber + "&pageSize=" + pageSize,
    //data: JSON.stringify({ "state": state,"pageNumber":pageNumber,"pageSize":pageSize }),
    dataType: "json",
    success: function (data) {

      $.each(data, function (index, emp) {
        $("#ReservationDesignation").append(emp.employee.designation.designationName);
      });


    },
    fail: function (error) {
      Console.Log(error);
    }
  })
}
</script>

While assigning the Designation name (emp.employee.designation.designationName) to #ReservationDesignation, I am getting a duplicate. ie, ArchitectArchitect. The actual result should be Architect.

I am not familiar with .each() function.

Can anyone help me to remove the duplicate from the result?

Thank You.

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
Phoenix
  • 285
  • 9
  • 28
  • What returns your url? – Orlando Charles Sep 08 '16 at 17:18
  • Have you seen this? http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Sjeiti Sep 08 '16 at 17:34
  • Sorry, didn't get you. – Phoenix Sep 08 '16 at 17:35
  • Yes Sjeiti, its for removing duplicates. What i want to know is how to prevent duplicates from getting created in the first place – Phoenix Sep 08 '16 at 17:36
  • You would remove the duplicates like how @Sjeiti linked to THEN you would append them to DOM. – Darkrum Sep 08 '16 at 17:41
  • The issue is your backend then, nothing wrong with the Javascript. What are you calling to? Somekind of REST framework I presume. You can always filter out duplicates in the frontend, but it's way better to fix it in the backend of course. ... -edit- oh wait... as @Darkrum indicated: there is something wrong with your Javascript but it's a whole other issue: never add stuff to DOM in a loop, and don't do queries inside the loop that you can do outside. Search for: documentFragment jquery – Sjeiti Sep 08 '16 at 18:09

0 Answers0