Remove occurrences of duplicate words in a string

前端 未结 9 2002
野性不改
野性不改 2020-11-27 07:37

Take the following string as an example:

var string = \"spanner, span, spaniel, span\";

From this string I would like to find the duplicate

9条回答
  •  醉话见心
    2020-11-27 08:05

    below is an easy to understand and quick code to remove duplicate words in a string:

    var string = "spanner, span, spaniel, span";
    
    
    var uniqueListIndex=string.split(',').filter(function(currentItem,i,allItems){
        return (i == allItems.indexOf(currentItem));
    });
    
    var uniqueList=uniqueListIndex.join(',');
    
    alert(uniqueList);//Result:spanner, span, spaniel
    

    As simple as this can solve your problem. Hope this helps. Cheers :)

提交回复
热议问题