2011-09-06 110 views
0

我使用jQuery 1.6动态创建和删除的JavaScript关联数组键和值

我不是JavaScript的这么好,我需要动态地创建具有2个动态参数数组:json.idjson.name

数组创建应导致:

[ 

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

然后,我需要能够删除json.idjson.id json.name ....

有没有人能告诉我如何与做到这一点所有浏览器的支持?

THX 将帖子

的逻辑是这样的:(希望这是明确的: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... 
} 
+0

2 dinamical paramas? –

+0

数据来自您用来创建此数组的数据来自哪里? –

+0

数据来自json ajax结果,对于每个ajax请求我有许多不同的json.id,然后对于每个json.id都有一个新的ajax调用,它检索许多与该json.id相关的不同json.name。我是 – sbaaaang

回答

1
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 
     } 
     } 
    }); 

注意,此示例假设你代替数组JSON对象,所以它是非常更容易和更快速地删除子对象或属性...

+0

无法理解此变量myJson = {'id39': { '名': '耶稣', '年龄':33}};我没有静态参数 – sbaaaang

+0

,我需要创建dinamically :) – sbaaaang

+0

所以请写下一个简单的json数据示例,你会检索 – dmidz