On Nov 17, 8:57 am, Geuis <[EMAIL PROTECTED]> wrote:
> I've gotten to be fairly intermediate at consuming json objects, such
> as those I'm retrieving from web api's. However, I'm trying to push
> the boundaries of what I understand about JSON objects once the client
> has loaded them. I have been searching around google for the last
> couple of hours for what I think I want to know but I'm getting
> nowhere. Hoping some other js wizzes can help me out. This isn't
> directly a jQuery question, but I need to understand more about this
> to help me write better jQuery code.
>
> This is my sample data for the following questions:
>         var obj = {     "nodes" :[
>                                 {
>                                         type : 'a',
>                                         name : 'stuff'
>                                 },
>                                 {type : 'b'},
>                                 {type : 'c'}
>                           ]
>         };
>
> 1) How can I add new nodes to an existing object? I can reference
> obj.nodes[x].type for example. Now I want to add a new type to
> "nodes". I tried: obj += {type:'d'}; but got nothing usable. Except
> that obj.nodes.length went from 3 to 62.


  obj.nodes.push({type:'d'});


> 2) Is there a list of core Javascript methods that let you work with
> JSON objects? Something like .push, .pop, etc?

Methods belong to objects, the methods you mention belong to
Array.prototype and therefore instances of Array.


> 3) How do you search a JSON object? Suppose I want to find a group of
> nodes that all have the name "stuff", regardless of what their "type"
> property is?
> I know I can loop through an object using for(i in obj){}.

Since nodes is an array, you can iterate over it using a for loop (or
an each method) and check:

  var t = nodes[i];
  var stuffGroup = [];
  if ( t.name && t.name == 'stuff' ) {
    stuffGroup.push(t);
  }

You might want to make the relevant methods properties of some object
that you construct and pass it the JSON data to work with.


--
Rob

Reply via email to