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!
------------------
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!