shanish
shanish

Reputation: 1984

finding duplicate strings using contains

I have two textboxes for getting the billing item name and amount, after fillind these two fields and clicking on Add button, am storing the datas into a html table like,

$("#BillTable > tbody").find("tr:gt(0)").remove();
  for (var i = 0; i < feesItemList.length; i++) {          

          tab = "<tr><td class='close16' onclick='DeleteBillItem(" + i + ")'>" 
                   "</td><td>" + " <span class='bid'>" + feesItemList[i].ItemName
                   + " </span> </td><td>" + feesItemList[i].Amount + "</td></tr>";

          $("#BillTable > tbody").append(tab);

  }

here the item name should not be a duplicate one. so I just put a condition before tab like

if($('bid').contains(feesItemList[i].ItemName){
    alert("Duplicate ItemName");
}
else{
    tab +=............//
}

but its not woriking, how can I fix this, can anyone help me here, thanks in advance

Upvotes: 0

Views: 78

Answers (1)

user1263800
user1263800

Reputation:

$(function () {

    var data = [2, 5, 10, 20, 2, 5, 10, 25, 2, 5, 50, 20, 10];
    var myArray = new Array();
    var temp = data;

    function RemoveDublicates() {

        for (var i = 0; i < data.length; i++) {
            var dublicate = false;
            for (var j = i + 1; j < data.length; j++) {
                if (data[i] == temp[j]) {
                    dublicate = true;
                    break;
                }
            }

            if (!dublicate) {
                myArray.push(data[i]);
            }

        }
    }
    //print
    for (var k = 0; k < myArray.length; k++) {
        $('body').append(myArray[k] + "<br/>");
    }

});

before you run your for loop put this code to remove any duplicate values the array have.

Upvotes: 1

Related Questions