Remove occurrences of duplicate words in a string

前端 未结 9 1999
野性不改
野性不改 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:16

    // Take the following string
    var string = "spanner, span, spaniel, span";
    var arr = string.split(", ");
    var unique = [];
    $.each(arr, function (index,word) {
        if ($.inArray(word, unique) === -1) 
            unique.push(word);
    
    });
    
    alert(unique);
    

    Live DEMO

提交回复
热议问题