Web Analytics Made Easy -
StatCounter need to find index of object in multidimensional array - CodingForum

Announcement

Collapse
No announcement yet.

need to find index of object in multidimensional array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    sorry that should be


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

    Comment


    • #3
      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; Aug 27, 2011, 05:34 PM.

      Comment

      Working...
      X