4

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)
Rick
  • 16,612
  • 34
  • 110
  • 163
  • 1
    Could we a see a sample of your JSON output? – gpmcadam Jul 24 '10 at 19:05
  • I'm a javascript newbie.. I can't figure out how to get the actual JSON output.. its being generated by cakephp.. any advice on how to go about this? I am searching on show json output but nothing of use is coming up.. thanks for your help in this – Rick Jul 24 '10 at 19:11
  • 1
    can you post alert(typeof jsonarray+'\n'+jsonarray.length+'\n'+jsonararray); – mplungjan Jul 24 '10 at 19:14
  • Thanks.. I updated the post with the info the last one, alert(jsonarray) doesn't work – Rick Jul 24 '10 at 19:24
  • 1
    @Rick: If the JSON data is coming from CakePHP output, can you not navigate to the page directly and see the output? – gpmcadam Jul 24 '10 at 19:27
  • Yes :), I figured out how to do it, just by echoing it in a normal way rather than the way it was doing to send it to javascript.. I edited the OP above – Rick Jul 24 '10 at 19:30
  • What's an example, given the test data above, of the kind of "value in an array" that you'd be searching for? – Ken Redler Jul 25 '10 at 18:23
  • well, the purpose behind this is so that, for any value, I can find its ID (for the purpose of this example, lets say "divid") so that when I send the data back to the server I can update based on using this id – Rick Jul 25 '10 at 23:22

2 Answers2

1

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
  • 23,863
  • 8
  • 57
  • 69
  • 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 Aug 07 '10 at 21:31
0

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:

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)

Community
  • 1
  • 1
Rick
  • 16,612
  • 34
  • 110
  • 163