2015-11-27 41 views
-3

假設我有一個從服務器結構如下在JavaScript中創建多個字典從單個單一的JSON響應?

var data={ 
    "Data1": { 
     "height": 39, 
     "weight": 62, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#00ff00", 
     "radius": 9.5, 
     "color_srv": "#ffff00" 
    }, 
    "Data2": { 
     "height": 0, 
     "weight": 40, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#000000", 
     "radius": 2.5, 
     "color_srv": "#ff0000" 
    } 
    } 

我想這個數據字典的一個字典某些數據分成兩個,同時保持結構的JSON響應。對於例如

var data_height = { 
    "Data1":{ 
     "height": 39, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#00ff00", 
     "radius": 9.5, 
    }, 
    "Data2":{ 
     "height": 0, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#000000", 
     "radius": 2.5, 
    } 
    } 
    var data_weight = { 
    "Data1":{ 
     "weight": 39, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color_srv": "#00ff00", 
     "radius": 9.5, 
    }, 
    "Data2":{ 
     "weight": 0, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color_srv": "#000000", 
     "radius": 2.5, 
    } 
    } 

上面兩個字典有不同的用途,所以得到統一的結果後,我想如何將後端的單個數據拆分爲兩個不同的字典。

編輯

這是我想這樣做,但它會拋出錯誤

解決方案1:

var serve={},live={}; 
     for(d in data){ 
     pname = d.split(':')[0]; 
     serve['pname'].radius= data[d].radius; 
     serve['pname'].center= data[d].center; 
     serve['pname'].color= data[d].color_srv; 
     live['pname'].radius= data[d].radius; 
     live['pname'].center= data[d].center; 
     live['pname'].color= data[d].color; 
     serve['pname'].numbers= data[d].serving; 
     live['pname'].numbers= data[d].living; 
     serve['pname'].place= pname; 
     live['pname'].place= pname; 
     } 

EDIT2

解決方案2:

var serve={},live={}; 
    for(d in data){ 
     pname = d.split(':')[0]; 
     serve['radius']= data[d].radius; 
     serve['center']= data[d].center; 
     serve['color']= data[d].color_srv; 
     live['radius']= data[d].radius; 
     live['center']= data[d].center; 
     live['color']= data[d].color; 
     serve['numbers']= data[d].serving; 
     live['numbers']= data[d].living; 
     serve['place']= pname; 
     live['plcae']= pname; 
    } 

上述兩種解決方案似乎都不起作用。

+0

您需要添加你已經嘗試過你的問題的代碼。我們希望看到你已經把努力放在第一位。 – Andy

+0

@這是我嘗試過的。 l – xxCodexx

+0

stringify和解析並賦值給新變量,然後,如果真的需要,刪除不需要的屬性。 –

回答

0

正如妮娜所說,只需克隆對象並從每個對象中刪除不需要的屬性即可。在這裏,我使用了reduce,最初的對象是data_heightdata_height屬性。

var clone = function (obj) { return JSON.parse(JSON.stringify(obj)); } 

var output = Object.keys(data).reduce(function (p, c) { 
    var obj = data[c]; 
    p.data_height[c] = clone(obj); 
    delete p.data_height[c].weight; 
    delete p.data_height[c].color_srv; 
    p.data_weight[c] = clone(obj); 
    delete p.data_weight[c].height; 
    delete p.data_weight[c].color; 
    return p; 
}, { data_height: {}, data_weight: {} }); 

輸出

{ 
    "data_height": { 
    "Data1": { 
     "height": 39, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#00ff00", 
     "radius": 9.5 
    }, 
    "Data2": { 
     "height": 0, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "color": "#000000", 
     "radius": 2.5 
    } 
    }, 
    "data_weight": { 
    "Data1": { 
     "weight": 62, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "radius": 9.5, 
     "color_srv": "#ffff00" 
    }, 
    "Data2": { 
     "weight": 40, 
     "shape": { 
     "length": 19, 
     "width": 72 
     }, 
     "radius": 2.5, 
     "color_srv": "#ff0000" 
    } 
    } 
} 

DEMO