Jquery check if array contains duplicate string

前端 未结 4 969
盖世英雄少女心
盖世英雄少女心 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:25

    This is working for me:

    var reportRecipients = ['AAA', 'XYZ', 'AAA', 'ABC', 'XXX', 'XYZ', 'PQR'];
    var recipientsArray = reportRecipients.sort(); 
    
    var reportRecipientsDuplicate = [];
    for (var i = 0; i < recipientsArray.length - 1; i++) {
        if (recipientsArray[i + 1] == recipientsArray[i]) {
            reportRecipientsDuplicate.push(recipientsArray[i]);
        }
    }
    

    Hope that helps.

提交回复
热议问题