+ Start a Discussion
Rama_SFDCRama_SFDC 

How to remove duplicate values in server side controller in lightning

Hi All ,
I want to display all the contact names  in a drop down list but in contact list i need to remove duplicate Names .How can we achive this requirment .please find below code

public static list<contact> getconfigvalues(){
       
    return [select id ,name from contact ];
}


var action = component.get("c.getconfigvalues");
        action.setCallback( this, function(response) {
            var nameresult= response.getReturnValue();
            var state = response.getState();      
            if (state === "SUCCESS") {
         
                for(var key in nameresult){
                    namelist.push(nameresult[key[0]].Name);
                    
                }  
                component.set("v.Name",namelist) ; 

How to remove duplicate name from the namelist or Name attributr ?
Best Answer chosen by Rama_SFDC
Raj VakatiRaj Vakati
try this code
 
public static List<String> getconfigvalues(){
	Set<String>  ret = new  Set<String> ();
List<Contact> cons =  [select id ,name from contact ];
for(Contact c :cons){
	ret.add(c.Name);
}

List<String> uniqueValue = new List<String>();
uniqueValue.addAll(ret);

return uniqueValue ;

}
 
var action = component.get("c.getconfigvalues");
        action.setCallback( this, function(response) {
            var nameresult= response.getReturnValue();
            var state = response.getState();      
            if (state === "SUCCESS") {
         
                component.set("v.Name",nameresult) ; 
			}

 

All Answers

Raj VakatiRaj Vakati
public static Set<String> getconfigvalues(){
	Set<String>  ret = new  Set<String> ();
List<Contact> cons =  [select id ,name from contact ];
for(Contact c :cons){
	ret.add(c.Name);
}
return ret ;

}
 
var action = component.get("c.getconfigvalues");
        action.setCallback( this, function(response) {
            var nameresult= response.getReturnValue();
            var state = response.getState();      
            if (state === "SUCCESS") {
         
                component.set("v.Name",nameresult) ; 
			}

 
Rama_SFDCRama_SFDC
Hi Raj , 
Thanks for your reply .

i was tried this approch earlier ,AuraEnabled methods do not support return type of Set<String>   .
is any alternative ways to remove duplicate values and i'm iterating  4 contacts fields for different drop down list.For all the fields i need to remove duplicate values .How can we achive this , please suggest any good approch this requirment


Thanks 
Raj VakatiRaj Vakati
try this code
 
public static List<String> getconfigvalues(){
	Set<String>  ret = new  Set<String> ();
List<Contact> cons =  [select id ,name from contact ];
for(Contact c :cons){
	ret.add(c.Name);
}

List<String> uniqueValue = new List<String>();
uniqueValue.addAll(ret);

return uniqueValue ;

}
 
var action = component.get("c.getconfigvalues");
        action.setCallback( this, function(response) {
            var nameresult= response.getReturnValue();
            var state = response.getState();      
            if (state === "SUCCESS") {
         
                component.set("v.Name",nameresult) ; 
			}

 
This was selected as the best answer
Rama_SFDCRama_SFDC
Thanks Raj , above approch working fine for one field and i'm looking for multiple fieldss how to handle 

Please correct me where did i mistake ,

var action = component.get("c.getconfigvalues");
        action.setCallback( this, function(response) {
            var nameresult= response.getReturnValue();
            var state = response.getState();      
            if (state === "SUCCESS") {
              var  namelist =[ ]; 
              var  phonelist =[ ]; 
              var  areacodelist =[ ]; 
                for(var key in nameresult){
                    namelist.push(nameresult[key[0]].Name);
                    phonelist .push(nameresult[key[0]].phone);
                    areacodelist .push(nameresult[key[0]].areacode__c);  
                 }       
             }  
                component.set("v.Name",namelist) ; 
                component.set("v.phone",phonelist ) ; 
                component.set("v.areacode",areacodelist ) ;