2011-09-06 78 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