Click to See Complete Forum and Search --> : how to grab every unique value out of an array in javascript.


antonielloj
05-13-2003, 10:56 AM
hey all. ive got a .js file that essentially stores a bunch of values in the following manner:

------------------

var index=-1;
var name = new Array();
var value = new Array();

index++;
name[index] = "item" + index;
value[index] = 22;

index++;
name[index] = "item" + index;
value[index] = 40;

index++;
name[index] = "item" + index;
value[index] = 22;

index++;
name[index] = "item" + index;
value[index] = 60;

index++;
name[index] = "item" + index;
value[index] = 40;

etc etc etc

---------

i want to write a function that traverses the entire array and stores every unique value in an array called, for example, valueArray. im assuming this is done using some sort of a double for-loop, but i cant seem to wrap my head around the most efficient/elegant way to do this.

var valueArray = new Array();
valueArray[0] = value[0];
for (var i=1 ; i<name.length ; i++) {

//now make another for loop that compares value[i] to every other value in valueArray. if it is unique, then store the value in valueArray. but how do you set up this inner for loop???

}

my trouble is with setting up the inner for loop. any suggestions? am i going about this entirely wrong? i feel like theres a very simple solution here that im simply not seeing.

any help would be greatly appreciated.
cheers!

Jona
05-13-2003, 11:09 AM
I'm not sure, but I think....

var valueAry = new Array(value.length); //length of the array is now the same as the length of the value array

for(i=0;i<valueAry.length;i++){ //valueAry's length is the same as value's length, so we can use it instead

valueAry[i] = value[i]; // set each value of the valueAray to the corresponding variable in the value array

if(value[i]==valueAry[i]){ // this code is not needed, but I put it in here just so you can see how to check and see if it's got the same values
alert(value[i]+" is equal to "+valueAry[i]);
return true;
} else { alert(value[i]+" is NOT equal to "+valueAry[i]); }
} // close the for() loop

khalidali63
05-13-2003, 11:19 AM
Hey jona...

don't you think this would always return true match

valueAry[i] = value[i];
if(value[i]==valueAry[i]){

khalidali63
05-13-2003, 11:22 AM
Ok here is th elogical flow for this threads answer.

1.create an array with all the values.
2. create a temporary array (empty)

3. loop through the main array
4.at each index
5.loop through the temp array and 6.match the value arrays current index with all the values in the temp array.
7.if matched don't inser else insert the main arrays index value in the tmep array

Jona
05-13-2003, 11:25 AM
Originally posted by khalidali63
Hey jona...

don't you think this would always return true match

valueAry[i] = value[i];
if(value[i]==valueAry[i]){

Originally posted by Jona
// this code is not needed, but I put it in here just so you can see how to check and see if it's got the same values

I put the if/else statements in there just so he knew how to check for them to see if the values were the same. Obviously, setting them and then checking them for the values you just set will return true, but I was giving an example.

antonielloj
05-13-2003, 12:06 PM
thanks khalidali63. i wound up doing basically what you said. i just added a flag variable that sets to false if it ever finds a match in the inner for-loop. then, in the outer loop, i store the value of i only if the flag is still true. then, right before exiting the outer for loop, i reset the flag. thanks for the walk-through. i dont know why i couldnt figure it out... rough morning i guess... :)

cheers!
j

khalidali63
05-13-2003, 12:13 PM
Originally posted by antonielloj
. rough morning i guess... :)
j

No worries, we all have those days

:D

Charles
05-13-2003, 01:00 PM
Or you could use an object to create an associative array of booleans.

<script type="text/javascript">
<!--
Array.prototype.getUniqueValues = function () {
var hash = new Object();
for (j = 0; j < this.length; j++) {hash[this[j]] = true}
var array = new Array();
for (value in hash) {array.push(value)};
return array;
}

alert(['fee', 'fie', 'foe', 'fum', 'fee', 'fie', 'foe', 'fum'].getUniqueValues());
// -->
</script>