Dynamically create and delete JavaScript associative array key and values
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,
开发者_如何学JAVAjson.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...
}
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...
精彩评论