-
remove duplicates and sort array
I have a multi level array.
I want to remove duplicates from the array and then sort it.
Problem is I want to do this to different columns of the array at different times.
So, first time I sort by workinstructure[i][0] and remove any duplicates in [i][0], next time I need to do the same but for column [i][3]
Code:
//create copy of the array to work with
var workingstructure = structure;
//initialise
if (document.getElementById) {
document.getElementById("Division").disabled=true;
document.getElementById("DivisionalLevels").disabled=true;
document.getElementById("BusUnitLevels").disabled=true;
document.getElementById("RegionalLevels").disabled=true;
document.getElementById("CostCentreLevels").disabled=true;
}
//sort the array, remove duplicates, etc.
workingstructure.sort; //i actually need to sort by different columns here, in this example it's [i][0] of the array
//for (i = 0; i < workingstructure.length; i++) {
// if (workingstructure[i][0] == some duplicate check here) {
// workingstructure.splice(i);
// }
//}
//fill with data from the copy javascript array
for (i = 0; i < workingstructure.length; i++) {
var optn = document.createElement("OPTION");
optn.text = workingstructure[i][1];
optn.value = workingstructure[i][0];
document.getElementById("Division").options.add(optn);
}
//destroy the array
workingstructure = null;
-
Re: remove duplicates and sort array
What exactly is your problem, the removing of the duplicates? Or knowing which column is the next one?
-
Re: remove duplicates and sort array
How to remove duplicates from the array.
Example data:
1,Jones, SY300
2,SMith, SY300
1,ABAB, Sy400
Sometimes, I need to check column 1 and remove duplicates leaving me with:
1,Jones, SY300
2,SMith, SY300
Sometimes, I need to check column 3 and remove duplicates leaving me with:
1,ABAB, Sy400
-
Re: remove duplicates and sort array
To remove duplicates is easy. Take a look at an example from my website.
-
Re: remove duplicates and sort array
but doesnt that look through the whole array?
How do I tell it to delete duplicates from column 2 only ?
See data example above
Code:
//create copy of the array to work with
var workingstructure = structure;
//initialise
if (document.getElementById) {
document.getElementById("Division").disabled=true;
document.getElementById("DivisionalLevels").disabled=true;
document.getElementById("BusUnitLevels").disabled=true;
document.getElementById("RegionalLevels").disabled=true;
document.getElementById("CostCentreLevels").disabled=true;
}
//fill with data from the copy javascript array
unique(workingstructure[1],false);
for (i = 0; i < structure.length; i++) {
var optn = document.createElement("OPTION");
optn.text = workingstructure[i][1];
optn.value = workingstructure[i][0];
document.getElementById("Division").options.add(optn);
}
//destroy the array
workingstructure = null;
-
Re: remove duplicates and sort array
Quote:
Originally Posted by JACKWEBS
but doesnt that look through the whole array?
Not a whole two-dimensional array, but it does an array. Just pass the second dimension to it.
-
Re: remove duplicates and sort array
But how do I do that?
If I pass array[2], it will think I'm talking about the 3rd row.
If I pass [i][2], it won't know which row to use.
Am I supposed to loop it? for i=0, i<array.length, i++
then use your function every time?
-
Re: remove duplicates and sort array
You were close in your first try. Notice it doesn't clean the array, it returns a new array without the duplicates.
Code:
workingstructure[1] = unique(workingstructure[1], false);
-
Re: remove duplicates and sort array
just says object doesn't support this property or method?
Also, the FirefOx debugger says tempArray[ii].toLowerCase is not a function
Code:
//create copy of the array to work with
//var workingstructure = structure;
workingstructure = new Array;
//initialise
if (document.getElementById) {
document.getElementById("Division").disabled=true;
document.getElementById("DivisionalLevels").disabled=true;
document.getElementById("BusUnitLevels").disabled=true;
document.getElementById("RegionalLevels").disabled=true;
document.getElementById("CostCentreLevels").disabled=true;
}
//fill with data from the copy javascript array
//workingstructure[0] = unique(structure[0],false);
workingstructure[1] = unique(structure[1],false);
for (i = 0; i < structure.length; i++) {
var optn = document.createElement("OPTION");
optn.text = workingstructure[i][1];
optn.value = workingstructure[i][0];
document.getElementById("Division").options.add(optn);
}
//destroy the array
workingstructure = null;
-
Re: remove duplicates and sort array
The function toLowerCase() can fail if the variable is not a string. They only way we can truly help you is if you post the array as well. Can you please do so?
-
Re: remove duplicates and sort array
Aha.
That will be why. The first element is a number, all the rest are text:
array:
0 3,Infrastructure,SY400,SY410,SY414,ZACY
1 5,Regions,SY600,SY610,SY610,ZALY
2 3,Infrastructure,SY400,SY410,SY410,ZAVI
3 4,Management,SY300,SY340,SY344,ZBBC
4 1,Central Services,SY700,SY700,SY700,ZBDX
5 4,Management,SY300,SY340,SY345,ZBFP
6 3,Infrastructure,SY400,SY440,SY440,ZBHX
row7 etc.
row 8 etc.
can I just change the 1st element to text.
SO just put in "3"
??
-
Re: remove duplicates and sort array
Am I to assume that the 0-6 are the indexes of the first dimension and that rest of each line delimited by the commas are the second dimensions?
-
Re: remove duplicates and sort array
Quote:
Originally Posted by PeejAvery
Am I to assume that the 0-6 are the indexes of the first dimension and that rest of each line delimited by the commas are the second dimensions?
Yes, sorry.
So If you put in array[1][0] you get 5
array [2][2] you get SY400
the array[i][0] is put in as a number in javascript when I build the array, all the others are text as they have "" round them
-
Re: remove duplicates and sort array
All you needed to do was add the toString() function to the check.
Code:
<script type="text/javascript">
function unique(theArray, caseSensitive){
var inArray;
tempArray = [];
for(i = 0; i < theArray.length; i++){
inArray = false;
for(ii = 0; ii < tempArray.length; ii++){
if(caseSensitive){
if(tempArray[ii] == theArray[i]){inArray = true;}
}
else{
if(tempArray[ii].toString().toLowerCase() == theArray[i].toString().toLowerCase()){inArray = true;}
}
}
if(!inArray){tempArray[tempArray.length] = theArray[i];}
}
return tempArray;
}
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
];
alert(testArray[2]);
alert(unique(testArray[2]));
</script>
-
Re: remove duplicates and sort array
Something weird is definitely happening here.
When I run it, it seems to only list 1 row, the number 3.
Is that because I only use this line once?
workingstructure[0] = unique(structure[0],false);
For example, in the code above when you alert(TestArray[2]); it only alerts one row of the array. And shouldn't it pick out the value SY400? It is alreting the entire row.
unique only seems to be returning 1 row. Can't figure out where though
Code:
//create copy of the array to work with
//var workingstructure = structure;
var workingstructure = new Array();
//initialise
if (document.getElementById) {
document.getElementById("Division").disabled=true;
document.getElementById("DivisionalLevels").disabled=true;
document.getElementById("BusUnitLevels").disabled=true;
document.getElementById("RegionalLevels").disabled=true;
document.getElementById("CostCentreLevels").disabled=true;
}
workingstructure[0] = unique(structure[0],false);
//workingstructure[1] = unique(structure[1],false);
for (i = 0; i < structure.length; i++) {
var optn = document.createElement("OPTION");
optn.text = workingstructure[i][0];
optn.value = workingstructure[i][0];
document.getElementById("Division").options.add(optn);
}
//destroy the array
workingstructure = null;
Int his array:
Code:
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
];
I would expect unique to return 3,5,4, and 1
Yet it only returns the value 3 because it looks at the first row only when you put in array[0]
-
Re: remove duplicates and sort array
Quote:
Originally Posted by JACKWEBS
For example, in the code above when you alert(TestArray[2]); it only alerts one row of the array. And shouldn't it pick out the value SY400? It is alreting the entire row.
unique only seems to be returning 1 row. Can't figure out where though
What are you talking about? The code I posted only removes duplicates from one array. I have stated this many times. There is nothing strange about how it is working.
Quote:
Originally Posted by JACKWEBS
Sometimes, I need to check column 1 and remove duplicates leaving me with:
1,Jones, SY300
2,SMith, SY300
Sometimes, I need to check column 3 and remove duplicates leaving me with:
1,ABAB, Sy400
The above quote implies that you were just going to call it on certain arrays in the second dimension, NOT ALL. If you want it to remove the duplicates in EVERY second dimension array, you have to loop (for) through the first dimension while calling it upon the second.
-
Re: remove duplicates and sort array
I appreciate the advice but maybe we're at crossed paths.
I tried your example code and it only printed one value.
row column1 column2
0 A B
1 A B
2 B B
3 C C
if I run newarry[0] = unique(array[0],false) on this it only prints out A.
I need it to list A, B, C
It doesn't work when I loop it because it keeps overwriting the value. It thinks newarray is only 1 dimension.
In the unique function it needs to look through [i][0] then [i][1], then [i][2] and compare those doesn't it?
To find unique rows it has to look down the column but at the moment it is looking across and only doing one row.
This code should give me a list of distinct values but it only returns one:
Code:
//create copy of the array to work with
var workingstructure = new Array();
var arrDD = new Array();
for (i = 0; i < structure.length; i++) {
workingstructure[i] = structure[i][0];
}
//initialise
if (document.getElementById) {
document.getElementById("Division").disabled=true;
document.getElementById("DivisionalLevels").disabled=true;
document.getElementById("BusUnitLevels").disabled=true;
document.getElementById("RegionalLevels").disabled=true;
document.getElementById("CostCentreLevels").disabled=true;
}
arrDD = unique(workingstructure,false);
alert(arrDD.length);
for (i = 0; i < arrDD.length; i++) {
var optn = document.createElement("OPTION");
optn.text = arrDD[i][0];
optn.value = arrDD[i][0];
document.getElementById("Division").options.add(optn);
}
//destroy the array
workingstructure = null;
arrDD = null;
-
Re: remove duplicates and sort array
Are you saying that you are trying to compare 1 array and remove duplicates from a different array? :confused:
I'm sorry, but I can't understand you. When working with arrays there is no such thing as columns and rows, so please don't explain them in that way. Arrays contain indexes, they are not tables.
-
Re: remove duplicates and sort array
No, just one array.
If I understand correctly your code is supposed to loop through the relevant dimension of the array and return an array with out the duplicates.
Either I am passing in the index and dimension incorrectly or I am not using the function correctly?
By passing in unique(array[0],false) it seems to assume that I mean index 0 (row) whereas I am trying to get it to look through dimension 0 (column).
For example in this array:
Code:
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
];
I want to pass in the reference to [0] so it returns an arry with 3,5,4,1 instead of 3,5,3,4,1,4,3
except it only returns the value 3 and stops at that.
Do I need to loop through and run unique multiple times or do I need to loop through the returned array?
Either way I am not passing in the right references somewhere...
-
Re: remove duplicates and sort array
Quote:
Originally Posted by JACKWEBS
I want to pass in the reference to [0] so it returns an arry with 3,5,4,1 instead of 3,5,3,4,1,4,3
That is why I was not understanding you...because you do not understand the array. You mention "reference to [0]" but yet you refer to the first value of the indexes [0] through [6]. Let me clear that part up for you.
Code:
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"], // this whole line is testArray[0]
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"], // this whole line is testArray[1]
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"], // this whole line is testArray[2]
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"], // this whole line is testArray[3]
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"], // this whole line is testArray[4]
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"], // this whole line is testArray[5]
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"] // this whole line is testArray[6]
];
You see, in this array the values 3, 5, 3, 4, 1, 4, 3 are not indexes, but values for the second dimension.
Quote:
Originally Posted by JACKWEBS
so it returns an arry with 3,5,4,1 instead of 3,5,3,4,1,4,3
So then you aren't removing duplicate values because those 2nd dimension arrays are different. Are you just attempting to remove the arrays based upon if the value array[x][0] already occurred?
-
Re: remove duplicates and sort array
Yes.
My point is that when i try to use unique(array[0]) on this array:
Code:
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"], // this whole line is testArray[0]
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"], // this whole line is testArray[1]
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"], // this whole line is testArray[2]
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"], // this whole line is testArray[3]
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"], // this whole line is testArray[4]
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"], // this whole line is testArray[5]
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"] // this whole line is testArray[6]
];
it only returns the value 3.
I want it to return an array that has values 3,5,4, and 1.
ALternatively, when I want to use one of the others it should return:
SY400, SY600, SY700, and SY300.
So I think I need to call it like this unique(array[i][0]) but when I loop it...the function will only look in the 2nd dimension.
It needs to compare each index against the next index, which I can't get it to do.
So, it needs to compare array[0][0] with array [1][0] then with array [2][0] and return an array without duplicates.
-
Re: remove duplicates and sort array
Quote:
Originally Posted by JACKWEBS
My point is that when i try to use unique(array[0]) on this array:...it only returns the value 3.
You are making no sense. If you alert unique[0] it returns the whole array because there are no duplicates in testArray[0].
The code alerts you with "3,Infrastructure,SY400,SY410,SY414,ZACY". That is exactly how it should function each of those returned are values within the second dimension. The two following code examples are the exact same.
Code:
var testArray = [
[3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
[5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
[3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
[4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
[1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
[4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
[3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
];
Is identical to...
Code:
var testArray = new Array();
testArray[0][0] = "3";
testArray[0][1] = "Infrastructure";
testArray[0][2] = "SY400";
testArray[0][3] = "SY410";
testArray[0][4] = "ST414";
testArray[0][5] = "ZACY";
...
Quote:
Originally Posted by JACKWEBS
So I think I need to call it like this unique(array[i][0])
No. That is invalid. testArray[x][x] is not an array, it is a value. testArray is an array, testArray[x] is also a value, but since it is only a 2 dimensional array, that means that testArray[x][x] is a value. Therefore, you cannot call the function unique() on a value because it was written for an array.
-
Re: remove duplicates and sort array
So, how do I compare the values and remove duplicates?
When I pass in the array as it is, how does the function know which values to compare in the 2nd dimension? I don't think it can.
So, I can create a new array with only one dimension
array[0] = oldarray[0][0]
array[1] = oldarray[1][0]
array[2] = oldarray[2][0]
But it has to compare each of those values against each other?
Will that work? Then I pass in unique(array, false) and it will return an array with all duplicates removed?
-
Re: remove duplicates and sort array
So is what you are attempting to do is remove duplicates of one second dimension array from a completely different second dimension array?
If this is the case, it is possible. All you would have to do is instead of looping through the tempArray in my code, is loop through the other second dimension with which you want to compare for duplicates.