Jquery check if array contains duplicate string

前端 未结 4 1011
盖世英雄少女心
盖世英雄少女心 2021-01-14 11:56

How to check if array contains a duplicate string , i have validateArray = [\'sa\',\'sa\',\'yu\'] i have used the following function from SO but same not working for me.

4条回答
  •  青春惊慌失措
    2021-01-14 12:27

    Please try this, if the sort function is not working well :

        var all_list= ['sa','sa','yu'] 
        var duplicates_list = [];
        var unique_list = [];
        $.each(all_list, function(key, value){
                if($.inArray(value, unique_list ) == -1){
                    unique_list.push(value);
                }else{
                    if($.inArray(value, duplicates_list ) == -1){
                        duplicates_list.push(value);
                    }
                }
        });
    
    //duplicated elements 
    console.log(duplicates_list )
    

提交回复
热议问题