Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need to find index of object in multidimensional array

    I have an array "arr" that is an array of objects. Each object has the same 7 properties.

    I want to find the index of the object with a property that matches a certain value x in the array arr. How can i accomplish this?

    The array has hash tables associated with it.

    arr [ obj [ i ] . property1 + "_" + obj [ i ] . property2 ] = arr [ i ] ;

    so whats the index of the object where .property1 = x ?

    Thanks, g

  • #2
    New Coder
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry that should be


    arr [ arr [ i ] . property1 + "_" + arr [ i ] . property2 ] = arr [ i ] ;

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts

    Smile

    Code:
    <script>
    var x = 12;
    arr = new Array();
    MyConstuctor = function(a){
     this.property1 = a.shift();
     this.property2 = a.shift();
     this.property3 = a.shift();
     this.property4 = a.shift();
     this.property5 = a.shift();
     this.property6 = a.shift();
     this.property7 = a.shift();
    }
    
    arr[0] = new MyConstuctor([1,2,3,4,5,6,7]);
    arr[1] = new MyConstuctor([8,9,10,11,12,13,14]);
    arr[2] = new MyConstuctor([15,16,17,18,19,20,21]);
    
    
    for(var i = arr.length;i--;)
     for(var prop in arr[i])
      if(arr[i][prop] == x)
       alert("array arr index " + i +" contains an Object\n that has a property whose value is " + x)
    </script>
    I don't understand the part about the hash table ?

    Last edited by DaveyErwin; 08-27-2011 at 09:34 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •