I'm using jQuery 1.6

I'm not so good on JavaScript and I need to dynamically create an array having 2 dynamic parameters: json.id and json.name.

the array creation should result in :

[

[json.id]
         [ 
           json.name,
             json.name,
              json.name,
                etc .....
         ]
[json.id]
          [
           json.name, 
             json.name,
               json.name,
                  etc ....
         ]
etc ...
]

Then I need to be able to delete a json.id or a json.id json.name ....

Is there anyone can show me how to do this with all browser's support?

thx [EDITED]

the logic is this: (hoping it's clear :P )

    //first ajax call result:
    [{json.id_parent:json.name,json.id_parent:json.name,json.id_parent:json.name, etc..}]

    //second ajax call results passing the json.id_parent:
    [{json.id_parent_child:json.name,json.id_parent_child:json.name,json.id_parent_child:json.name,etc...}]
    //now for each call = id_parent create an associative array: 

{ 
id_parent:{id_parent_child,id_parent_child,etc ....},
id_parent:{id_parent_child,id_parent_child,etc ....},
etc...
}
share|improve this question

70% accept rate
2 dinamical paramas? – Robin Maben Sep 6 '11 at 12:05
Where will the data come from that you're using to create this array? – Richard Ev Sep 6 '11 at 12:05
the data come from a json ajax result , for each ajax request i have many different json.id, then for each json.id there is a new ajax call which retrieves many different json.name related to that json.id – Ispuk Sep 6 '11 at 12:12
I am sorry, in your edit, did you mentionned REAL datas ? all I see is a data pattern, it is hard to understand what you try to parse...please type one typical data line (with some real properties and real values) for both ajax responses... – dmidz Sep 6 '11 at 13:10
response should be like : "result":{"id0":{"name":"A"},"id1":{"name":"B"},...} – dmidz Sep 6 '11 at 13:27
feedback

1 Answer

    var myJson = {'id39':{'name':'Jesus','age':33}};
    var idDel = 'id39';
    delete myJson[idDel];// delete the complete reference with id
    var propDel = 'name';
    delete myJson[idDel][propDel];// delete only the property 'name' of the reference with id
// parsing complete json
    $.each(myJson, function(key, obj){// parse objects
      if(condition){
        delete myJson[key];// delete reference
      }
      $.each(obj, function(propName, val){// parse properties
        if(condition){
          delete obj[propName];// delete property
        }
      }
    });

Note, this sample assume you used json objects instead arrays so it is much easier and faster to remove sub objects or properties...

share|improve this answer
can't understand this var myJson = {'id39':{'name':'Jesus','age':33}}; i have no static params – Ispuk Sep 6 '11 at 12:21
that i need to create dinamically :) – Ispuk Sep 6 '11 at 12:21
so please write down a simple json data sample you would retrieve – dmidz Sep 6 '11 at 12:24
the example you posted is ok :) just this var myJson = {'id39':{'name':'Jesus','age':33}}; can't be static for me ... i need to create that dinamically and away we go :) – Ispuk Sep 6 '11 at 12:26
yes but I am not sure to understand, please precise a piece of real data you retrieve, then we can see how to build dynamically the array you wish ;) – dmidz Sep 6 '11 at 12:28
show 2 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.