www.webdeveloper.com
Results 1 to 5 of 5

Thread: need to find index of element in multidimensional array

  1. #1
    Join Date
    Mar 2011
    Posts
    43

    need to find index of element 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. #2
    Join Date
    Mar 2011
    Posts
    43
    sorry that should be


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

  3. #3
    Join Date
    Apr 2006
    Location
    Perth
    Posts
    154
    That isolated line is very confusing to understand without context. More information is
    really needed to answer your question. It will also answer my question of, "What are
    you doing?"

    Code:
    arr=new Array(); // arr is an array
    i=0; // a loop counter but take one instance of it for now
    arr[i]="A_B"; // need to define the contents of arr[i]
    arr[i].property1="A"; // also need to define the two property's
    arr[i].property2="B";
    arr[arr[i].property1+"_"+arr[i].property2]=arr[i];
    
    // this last line would translates to:
    
    arr["A_B"]="A_B";
    
    // but only if I knew how you devised arr[i].property1="A";
    // which does not seem to work in my quick test.
    I think there is something missing in my logic; the something you need to supply.
    Colin Fiat Simple Solutions To Complex Problems

  4. #4
    Join Date
    Mar 2011
    Posts
    43
    I make an ajax request to get data which returns an array...., 3 rows are returned which are stored in resultArray. I want to put each array row into an an object then put them back into a new array

    Code:
    function quickObj ( a, b ) {
       this.property1 = a;
       this.property2 = b;
    }
    
    suppose resultArray[0][0] = "Cheerios";
    suppose resultArray[0][1] = "Cereal";
    suppose resultArray[1][0] = "Yogurt";
    suppose resultArray[1][1] = "Snack";
    suppose resultArray[2][1] = "Orange";
    suppose resultArray[2][0] = "Fruit";
    
    //...now want to put these into an object then put the objects into an array.
    
    var arr = new Array();
    for ( var i = 0; $i < numObjects; i++ ) { 
       myObj = new quickObj ( resultArray[i][0], resultArray[i][1] );
       arr.push(myObj);
       arr[myObj.property1 + "_" + myObj.property2] = arr[i];
    }
    
    //So now i have an array of objects. I can access the objects and their //properties for example
    
    arr["Cheerios" + "_" + "Cereal"].property1 = "Cheerios".
    What i want to know is how i can find the index number of the object in the array with property1 that matches a certain value. So, how can i find the index number of the object with property1 = "Yogurt"

    Is that clearer?

  5. #5
    Join Date
    Apr 2006
    Location
    Perth
    Posts
    154
    Yes, much clearer, thanks.

    I am guessing you want instant results and not a loop to search and count the data? I
    would want that so I'll show that.

    Code:
    <html>
    <script>
    function quickObj ( a, b, c ) {
       this.property1 = a;
       this.property2 = b;
       this.property3 = c;
    }
    resultArray=new Array(3);
    for(i=0;i<3;i++)
     resultArray[i]=new Array(2);
    
    resultArray[0][0] = "Cheerios";
    resultArray[0][1] = "Cereal";
    resultArray[1][0] = "Yogurt";
    resultArray[1][1] = "Snack";
    resultArray[2][1] = "Orange";
    resultArray[2][0] = "Fruit";
    
    var arr = new Array();
    for ( var i = 0; i < 3; i++ ) { 
       myObj = new quickObj ( resultArray[i][0], resultArray[i][1], i );
       arr.push(myObj);
       arr[myObj.property1 + "_" + myObj.property2] = arr[i];
    }
    document.write(arr["Yogurt" + "_" + "Snack"].property3);
    
    </script>
    </html>
    Colin Fiat Simple Solutions To Complex Problems

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles