|
-
January 24th, 2008, 07:54 AM
#16
Re: remove duplicates and sort array
 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.
 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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 24th, 2008, 08:31 AM
#17
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;
Last edited by JACKWEBS; January 24th, 2008 at 09:32 AM.
-
January 24th, 2008, 09:23 AM
#18
Re: remove duplicates and sort array
Are you saying that you are trying to compare 1 array and remove duplicates from a different array? 
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 24th, 2008, 11:44 AM
#19
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...
-
January 24th, 2008, 12:53 PM
#20
Re: remove duplicates and sort array
 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.
 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?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 24th, 2008, 02:19 PM
#21
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.
Last edited by JACKWEBS; January 24th, 2008 at 02:40 PM.
-
January 24th, 2008, 02:39 PM
#22
Re: remove duplicates and sort array
 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";
...
 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.
Last edited by PeejAvery; January 24th, 2008 at 02:43 PM.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 24th, 2008, 02:45 PM
#23
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?
Last edited by JACKWEBS; January 24th, 2008 at 02:55 PM.
-
January 25th, 2008, 08:31 AM
#24
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|