javascript : check for duplicates

hi experts

I am using EXT-Js 4.2 code to design some UI for my project.

I have set a renderer in my javascript code for a grid column

function firstnameRenderer(val) {       
if(same firstname occurs twice , then color the two firstname with orange){ 
return '<span style="color:orange;">' + val + '</span>';	
}else{
    return val;
} 
} 
....
....
{
        id:'myGrid',
        xtype : 'grid', 
        stripeRows: true,
        
        title: 'User Information',
        store: Ext.data.StoreManager.lookup('mystore'),         
        autoHeight: true,
        plugins: [ cellEditing ],
        columns: [            
            {header: "First Name", width: 100, dataIndex: 'firstname', renderer: firstnameRenderer, sortable: true}
             


...

Open in new window

I want to check if a same firstname appears twice, if yes color it with orange.
Any idea how firstnameRenderer() function above should change ?

Thanks
royjaydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

J NUnicorn wranglerCommented:
Will the jquery unique function work?
http://api.jquery.com/jquery.unique/
0
royjaydAuthor Commented:
I dont think so, i am using Ext-Js 4.2. Can you tell me how i can change the function
firstnameRenderer() in Ext-js

Thanks.
0
J NUnicorn wranglerCommented:
how is val formatted?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

royjaydAuthor Commented:
sorry i dint get your question.

I have something like this working for me right nowas a test.

function firstnameRenderer(val) {        
if(val == 'Angela') {
    alert(val);
      return '<span style="color:orange;">' + val + '</span>';
}else{
    return val;
}
}

columns: [
                        {header: "First Name", width: 100, dataIndex: 'firstname', renderer: firstnameRenderer, sortable: true}
        ],
0
J NUnicorn wranglerCommented:
Hi,

here is a link on github for a way to elminate duplicates in a string
https://gist.github.com/lsauer/1305056

otherwise i would break the string down into parts and compare them
0
J NUnicorn wranglerCommented:
val = 'jayme nagy jayme jayme';

function firstName(val){
     var trimmed = val.trim();

     //see if the string contains a space (jayme nagy)
     if(trimmed .indexOf(" ") > 0){
         //convert string into array
         var names = trimmed.split(" ");
		 
		 //replace first name
		 var other = trimmed.replace(names[0], "");
		
		//test for more similar names
		if(other.indexOf(names[0]) >0){
			//there is a duplicate	
			return '<span style="color:orange;">' + val + '</span>';
			console.log(val);
		}
    }
	
}
firstName(val);

Open in new window

0
leakim971PluritechnicianCommented:
var maco = {};
function firstnameRenderer(val) {
        if( maco[val] ) {  // check if it already exists (return null if not)
                maco[val] = true;
                return '<span style="color:orange;">' + val + '</span>';	
        }
        else {
               return val;
        } 
} 

Open in new window

0
royjaydAuthor Commented:
hi
I used the above code but looks like somethng is missing
I have
var maco = {};
function firstnameRenderer(val) {
    alert(val);
            if( maco[val] ) {  // check if it already exists (return null if not)            
            alert('Inside if');  
            maco[val] = true;
                return '<span style="color:orange;">' + val + '</span>';      
        }
        else {
             alert('Inside else');
               return val;
        }
}

val contain values
Angela
Michael
Michael
Kim

When val = Michael
alert('Inside if');   should pop up but its showing alert('Inside else');
0
leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/9mhv2z45/3/
var maco = {};
function firstnameRenderer(val) {
    if( !maco[val] ) {  // check if it already exists (return null if not)            
        alert('Inside else'); 
        maco[val] = true;
        return '<span style="color:orange;">' + val + '</span>';      
    }
    else {
        alert('Inside if');   
        return val;
    } 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
royjaydAuthor Commented:
thanks leakim971 and everyone.
0
royjaydAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.