-2

I'm trying to eliminate duplicate array values and get distinct values in knockout js.

My code is as follows

$.ajax({
    url: 'http://localhost:53489/api/datacollect/GetPatientUDData',
    type: "GET",
    dataType: 'json',
    success: function(data) {
        for (var i = 0; data.length; i++) {
            vModel.UDTableDesc.push(data[i]);
        }
    },
    error: function(xhr, textStatus, errorThrown) {
        console.log('Error in Operation');
    }
});

function viewModel() {
    var viewModel = this;
    viewModel.UDTableDesc = ko.observableArray();
    return viewModel;
}

var vModel = new viewModel();
ko.applyBindings(vModel, document.getElementById("MainTree"));

Any suggestions would be great!

janfoeh
  • 10,243
  • 2
  • 31
  • 56
Matt
  • 1
  • 1

1 Answers1

-1

Try checking for existence before you push an entry onto the array:

for (var i = 0; data.length; i++) {
  if (vModel.uDTableDesc.indexOf(data[i]) === -1) {
    vModel.UDTableDesc.push(data[i]);
  }
}
janfoeh
  • 10,243
  • 2
  • 31
  • 56