Search Unity

check array for duplicate values JS

Discussion in 'Scripting' started by Krodil, Apr 30, 2013.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    hey,
    I am loading from xml a NAME of an object and corresponding X,Y,Z values for the object.
    These are placed in separate arrays for me to better control the data.
    Some of these names are the same, but I am only interested in the last set where the name+x,y,z occur.

    What I struggle with is: I have 4(I also have 3 for rota. but lets not over complicate it) arrays where the are some of the names being duplicates.In the code below the NameArray is 't'.

    I wish to make an array where all the latest 't's are only represented. And their corresponding values as well

    e.g.
    UNIQUE NAME|X|Y|Z|
    UNIQUE NAME|X|Y|Z|
    UNIQUE NAME|X|Y|Z|
    UNIQUE NAME|X|Y|Z|
    etc.


    Code below shows where I gave up.
    Code (csharp):
    1. var CheckEntryArray : Array = new Array();
    2.     //Instantiate all objects in XML with corresponding positions and rotations with final placements of objects
    3.     for(var i : int=0; i < t.length; i++)
    4.     {
    5.         Instantiate(spawn, Vector3(xt[i], yt[i], zt[i]), Quaternion.Euler(xr[i],yr[i],zr[i]));
    6.         spawn.name = t[i];
    7.        
    8.         CheckEntryArray.Push((t[i]),(xt[i]),(yt[i]),(zt[i]));
    9.        
    10.         //print(CheckEntryArray);
    11.        
    12.     }
     
    Last edited: Apr 30, 2013
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should never use the JS Array class; use a generic List instead. You can look in the docs for List for functions that will be useful, such as Contains.

    --Eric
     
  3. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    thanks for the tip.
    I am now using Lists.
    The case is that I have a list with name and the coordinates in 3 float lists.
    I only want latest entry of the same NAME so I reverse my list and check for duplicates..like below


    Code (csharp):
    1.  
    2. var Name :List.<String> = new List.<String>();
    3. function Start(){
    4. Name = GameObject.Find("_ScriptHolder").GetComponent(xml).nameList;
    5.  
    6. }
    7. var updatedNameList : List.<String> = new List.<String>();
    8. function CheckName()
    9. {
    10.         Name.Reverse();//Reversing order of strings('same' as looping backwards)
    11.        
    12.         for(var nameCheck:String in Name)
    13.         {
    14.            
    15.            
    16.             if(updatedNameList.Contains(nameCheck))
    17.             {
    18.                
    19.                
    20.                
    21.             }
    22.             else
    23.             {
    24.                 updatedNameList.Add(nameCheck);
    25.                 //updatedXList.Add(xposCheck);
    26.                 print(nameCheck);
    27.                
    28.             }
    29.         }
    30.         updatedNameList.Reverse();
    31.         print(updatedNameList.Count);
    32.        
    33. }
    Now what I cant seem to figure out is:
    to only add values from the other lists at the corresponding index. to new lists of floats.

    I tried looping two lists in same for() <-- unsuccessful
    and nested would count wrongly

    any ideas? :)

    best,
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Use a Dictionary or a HashSet. Also for the sake of speed - don't reverse a list and then enumerate, just use a for loop with an index that counts down.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Probably a Dictionary would be best, but if you need a List, I'd suggest using a class which contains the name and the coordinates, instead of separate parallel lists. You can use Linq to check the name property. For example, in a List of Vector2s, this gets all the entries where the x property is .5:

    Code (csharp):
    1. import System.Linq;
    2.  
    3. function Start () {
    4.     var myList = new List.<Vector2>([Vector2(.5, 1.0), Vector2(.7, 1.1), Vector2(.5, 1.1)]);
    5.     var matches = myList.Where (function(item) item.x == .5);
    6.     for (match in matches) {
    7.         Debug.Log (match);
    8.     }
    9. }
    --Eric