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)
8
  • 1
    Could we a see a sample of your JSON output? Commented Jul 24, 2010 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 Commented Jul 24, 2010 at 19:11
  • 1
    can you post alert(typeof jsonarray+'\n'+jsonarray.length+'\n'+jsonararray); Commented Jul 24, 2010 at 19:14
  • Thanks.. I updated the post with the info the last one, alert(jsonarray) doesn't work Commented Jul 24, 2010 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? Commented Jul 24, 2010 at 19:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.