Reply
Not allowing duplicate values to an array
Old 06-18-2007, 01:40 AM Not allowing duplicate values to an array
amox's Avatar
Novice Talker

Posts: 10
Name: Nash Field
Trades: 0
Hi,
How can I prevent duplicate values from getting stored into an array? I am trying to dynamically populate an array with data using the concat() function, but I do not want to allow duplicate selection to be stored in the array. Total noob here. Pls help! Thanks.
amox is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 06-18-2007, 01:11 PM Re: Not allowing duplicate values to an array
akratellio's Avatar
Experienced Talker

Posts: 30
Location: Berlin Germany Europe World
Trades: 0
Hi amox,
Code:
<script>
var entries = new Array("hi","hallo");
var entry = 'hi you';
var found = 0;
for(i=0;i<entries.length;i++){
    if(entries[i] == entry){
        ++found; // value was found
        break;
    }
}
if(found==0)
    entries.push(entry); // save new value
</script>
greetings akratellio
__________________
I'm always lying...
Ich lüge immer...
http://www.kcanada.de

Last edited by akratellio; 06-18-2007 at 01:30 PM..
akratellio is offline
Reply With Quote
View Public Profile Visit akratellio's homepage!
 
Old 06-18-2007, 02:41 PM Re: Not allowing duplicate values to an array
chrishirst's Avatar
Super Moderator

Posts: 26,558
Location: Blackpool. UK
Trades: 0
The standard concat() method only puts both arrays together, duplicates as well.
While we could create a function to handle this, it is just as simple to add a method to the Array Object.

Array.concat2();
Code:
<script type="text/javascript"> 
// Array.concat2()
// array concatenation method for arrays with duplicates.
// From C and S Design  www.candsdesign.co.uk. 

// define two functions that will become the method 
function checkExist(value,arr) {
for (var zz=0; zz<arr.length; zz++) {
		if ( arr[zz] == value)
		{
			return true;
			break
// leave function and return true if duplicate exists 
		}
}
// otherwise return false 
return false
}

// concatenate both arrays and check for duplicates 
function concat2(testArray,joinArray) {
	for (var zz=0; zz<testArray.length; zz++) {
// pass one array value and the array to join it into 
		if (checkExist(testArray[zz],joinArray) == false) {
// if duplicate is not found push the value onto the join array 
			joinArray.push(testArray[zz]);
		}
	}
}

// create our new function as a method for the Array object		
Array.prototype.concat2 = concat2;
</script>
How to call the new method
Code:
<script type="text/javascript"> 
// define and populate two arrays 
Names = new Array("Jack","Jill","Hill","Bucket","Pail","Water");
Arr2 = new Array("Bill","Ben","Little Weed","Bucket","Water","Plant Pot","Rabbit");

// pass both arrays to the method 
Names.concat2(Arr2,Names)
//document.getElementById('content').innerHTML =  Names ;
</script>

Link to demo page Array.Concat2
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | Crowded Nightclub? | Bits & Bobs

Last edited by chrishirst; 06-18-2007 at 02:47 PM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-20-2007, 06:17 AM Re: Not allowing duplicate values to an array
amox's Avatar
Novice Talker

Posts: 10
Name: Nash Field
Trades: 0
Thanks a million!!

akratellio,
Useful piece of code! Since I'm using the same function to add items into the array, I tested your code and found out that I couldn't add more values to my array b'cos the variable "found" keeps incrementing. Therefore I modified it a bit. I reinitialize it so it could move on and start over.

Code:
if(found==0){
    entries.push(entry); // save new value
}
else{
    //i do something else here
    found=0;
}
Then I can continue to add more items into my array! Thanks!

chrishirst,
I am almost embarassed at your effort to explain the code to me. You went a step further and provided a test page for that! Thank you so much. Accept my bear hug?
amox is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Not allowing duplicate values to an array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.09563 seconds with 13 queries