views:

121

answers:

2

I'm struggling with this, I am still new to javascript and am trying the jquery $.inarray function as I need to be able to find a value in an array so that I can return other values from the same array "line", such as its ID.

When I try to use jquery's inarray function, like this, I just get back -1, saying that it doesn't exist when I know the value exists, nested within the array.. I'm not sure of how to approach this so that I can search for a value, any advice is greatly appreciated.

valuereturn = $.inArray("search_for_value", jsonarray) ;
alert (valuereturn)

EDIT 2:

Here is the result of my echo'ing the JSON from cakephp:

    {"getdata":[{"Uiemail":{"uiemail_id":"2","divid":"upd1","width":"200","height":"200","leftpos":"122","toppos":"122","cssgroup":"1","colortop":"","colorbottom":"","colorborder":"","borderwidth":"","zindex":""}},
{"Uiemail":{"uiemail_id":"3","divid":"upd2","width":"200","height":"200","leftpos":"333","toppos":"444","cssgroup":"1","colortop":"","colorbottom":"","colorborder":"","borderwidth":"","zindex":""}},
{"Uiemail":{"uiemail_id":"4","divid":"upd3","width":"200","height":"200","leftpos":"555","toppos":"466","cssgroup":"1","colortop":"","colorbottom":"","colorborder":"","borderwidth":"","zindex":""}}]} 

EDIT:

Also, here is the output of:

alert(typeof jsonarray+'\n'+jsonarray.length)
output= "object 3 "

I tried this also, but it doesn't give a value and makes errors on my page:

alert(jsonararray)
A: 

Well, for now I've decided to use an approach of flattening the arrays within javascript and pre-processing the data that way since I can't figure out how to do it in javascript.. I found this:

http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php

This should enable me to move on past this issue but I am interested if anyone knows how to accomplish what I am trying to do directly in javascript (search within the nested array)

Rick
A: 

I'm still not completely sure what you're asking, so here are some assumptions and a quickie solution.

  1. You're looking for uiemail_ids in that blob of data
  2. The blob could be arbitrarily deep
  3. If you find one, you want to get the value of the divid in the same "line"
  4. First one found is the winner

Now throw together a little recursive function:

function deepsearch ( blob, val ) { 
    var result = false;
    for( var item in blob ) {
        if( typeof blob[item] === 'object' ) {
            result = deepsearch( blob[item], val );
            if( result != false ) return result;
        } else if( blob[item] == val && item == 'uiemail_id' ) {
            // found item, blob = obj in which found
            result = blob.divid; // the divid from this "line"
            break; // assume first found wins            
        }
    }
    return result;
}      

Plopping in your exact JSON:

var arr = {"getdata":[{"Uiemail":...

We can now look for a uiemail_id of 4, and get the corresponding divid of upd3:

deepsearch( arr, '4'); // returns upd3
deepsearch( arr, '3'); // returns upd2

This is probably brittle, and certainly can be improved, but maybe it gives you an angle of attack.

Ken Redler
Thanks for the info.. I just gave up on this actually by just pre-breaking down the arrays into "normal" arrays on the PHP side then sending them over to javascript
Rick