1
 $(".search").keyup(function(){
     var val = this.value;

        $.ajax({
        type: "POST",
        url:  "search?entered=" + val,
        beforeSend: function(){    
            $(".search").css("background","#FFF");
        },
        success: function(data){
            for (var i = 0; i < data.length; i++) {
                $(".suggesstions").append("<ul>"+data[i]["category"]+" "+data[i]["productTitle"]+"</ul>");
             }

        }

        });

    });

Here is my code, I want to remove duplicate entries from list and append to.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
  • Possible duplicate of [Remove duplicate objects from an array using javascript](http://stackoverflow.com/questions/19501441/remove-duplicate-objects-from-an-array-using-javascript) – Jaffer Wilson Jul 13 '16 at 11:21
  • if data object is an Array than you can see this question http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Rajesh Kathiriya Jul 13 '16 at 11:21
  • i taken a object list from controller as a response..like success: function(data){ – Amol Birajdar Jul 13 '16 at 11:22
  • Can you please provide your full script from which you want to remove duplicate characters. OR If you can provide Fiddle it would be great. – Sunil Kumar Jul 13 '16 at 11:28

5 Answers5

1

First, you have to remove all duplicate entries from array , like this

 var myArr = [2, 5, 5, 7, 7, 8, 9, 9, 9, 1, 3, 3, 4, 5];
 var newArr = $.unique(myArr.sort()).sort();

for (var i = 0; i < newArr.length; i++) {
         //your code
}
rejo
  • 3,352
  • 5
  • 27
  • 34
1

I modified the code with remove duplicate value based on productTitle.

 success: function(data){
             var array = [],
             Finalresult = [];

           $.each(data, function (index, value) {
               if ($.inArray(value.productTitle, array) == -1) {
                array.push(value.productTitle);
                Finalresult.push(value);
              }
           });
         for (var i = 0; i < Finalresult.length; i++) {
                        $(".suggesstions").append("<ul>"+Finalresult[i]["category"]+" "+Finalresult[i]["productTitle"]+"</ul>");
                     }
        console.log(Finalresult);

    }
Dil85
  • 176
  • 1
  • 1
  • 14
0

One Liner

var arrOutput = Array.from(new Set([1,2,3,4,5,5,6,7,8,9,6,7]));
alert(arrOutput);
zawhtut
  • 8,335
  • 5
  • 52
  • 76
0
let myArray = [2, 5, 5, 7, 7, 8, 9, 9, 9, 1, 3, 3, 4, 5];
 let arrayOutput = [];
            myArray?.map((c) => {
                if (!arrayOutput.includes(c)) {
                    arrayOutput.push(c);
                }
            });
console.log(arrayOutput)
0

let myArray = [2, 5, 5, 7, 7, 8, 9, 9, 9, 1, 3, 3, 4, 5];
     let arrayOutput = [];
                myArray?.map((c) => {
                    (!(arrayOutput.indexOf(c) > -1)) ? arrayOutput.push(c) : null;                
                });
    console.log(arrayOutput)