2017-10-20 80 views
-3

您好我需要解析一些結構來顯示一些數據的折線圖。我有這樣的數據結構,我需要將其轉換爲數組example_output:轉換一個數據結構到另一個

data = { 
    "el1": [{ 
     "date": "2017.01", 
     "data1": { 
      "series_1": { 
       "a": 10, 
       "b": 20, 
       "c": 50, 
       "d": 15, 
       "e": 8 
      }, 
      "Series_2": { 
       "yes": 5, 
       "no": 3 
      }, 
      "Series_3": { 
       "s": 2, 
       "n": 9 
      } 
     }, 
     "text": [{ 
      "t": "header", 
      "c": "text" 
     }, { 
      "t": "header2", 
      "c": "text2" 
     }] 
    }, { 
     "date": "2017.02", 
     "data1": { 
      "series_1": { 
       "a": 56, 
       "b": 23, 
       "c": 45, 
       "d": 69, 
       "e": 14 
      }, 
      "Series_2": { 
       "yes": 2, 
       "no": 1 
      }, 
      "Series_3": { 
       "s": 6, 
       "n": 4 
      } 
     }, 
     "text": [{ 
      "t": "header", 
      "c": "text" 
     }, { 
      "t": "header2", 
      "c": "text2" 
     }] 
    }, { 
     "date": "2017.03", 
     "data1": { 
      "series_1": { 
       "a": 15, 
       "b": 12, 
       "c": 10, 
       "d": 54, 
       "e": 4 
      }, 
      "Series_2": { 
       "yes": 20, 
       "no": 16 
      }, 
      "Series_3": { 
       "s": 9, 
       "n": 7 
      } 
     }, 
     "text": [{ 
      "t": "header", 
      "c": "text" 
     }, { 
      "t": "header2", 
      "c": "text2" 
     }] 
    } 
    ] 
}; 

,我需要OUTPUT像這樣chartist.js

var example_output = [{ 
labels: ['2017.01', '2017.02', '2017.03'], 
series: { 
    [10, 56, 15], 
    [20, 23, 12], 
    [50, 45, 10], 
    [15, 69, 54], 
    [8, 14, 4] 
}, 
labels: ['2017.01', '2017.02', '2017.03'], 
series: { 
    [5, 2, 20], 
    [3, 1, 16] 
}, 
labels: ['2017.01', '2017.02', '2017.03'], 
series: { 
    [2, 6, 9], 
    [9, 4, 7] 
},}] ; 

請從原來的和example_output比較數字更好地理解這應該是什麼樣子。我使用代碼分析這一點,但成纔出錯:

function parseData(data) { 
    var _newData = {}; 
    var allSeries = []; 
    data.elements.forEach(function(el){ 
    _newData[el.date] = el.info 
    if(allSeries.length==0) 
     allSeries = Object.keys(el.info); 
    }); 

    return allSeries.map(function(el) { 
    var obj = { 
     labels: [], 
     series: [] 
    }; 
    obj.labels = Object.keys(_newData); 
    Object.keys(_newData).forEach(function(_el) { 
     obj.series.push(Object.values(_newData[_el][el])); 
    }); 
    var _newSeries = []; 
    obj.series[0].forEach(function(el, i){ 
     _newSeries.push([el, obj.series[1][i]]); 
    }); 
    obj.series = _newSeries; 
    return obj; 
    }); 
} 
+0

[。在你的代碼中沒有JSON(http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/ ) – Quentin

+0

對不起,我的意思是var data是我的JSON – breakbit

+1

不。不要混淆JavaScript和JSON。它們不是同一件事。 – Quentin

回答

1

您可以使用array#forEacharray#reduce。使用第一個循環遍歷el1,然後使用reduce作爲series鍵,並將結果累積在output_example中。

注意:輸出中的series不正確。它應該是一個數組而不是對象。

const data = {"el1": [{"date": "2017.01","data1": {"series_1": {"a": 10,"b": 20,"c": 50,"d": 15,"e": 8},"Series_2": {"yes": 5,"no": 3},"Series_3": {"s": 2,"n": 9}},"text": [{"t": "header","c": "text"}, {"t": "header2","c": "text2"}]}, {"date": "2017.02","data1": {"series_1": {"a": 56,"b": 23,"c": 45,"d": 69,"e": 14},"Series_2": {"yes": 2,"no": 1},"Series_3": { "s": 6,"n": 4 }},"text": [{"t": "header","c": "text"}, {"t": "header2","c": "text2"}]}, {"date": "2017.03","data1": {"series_1": {"a": 15,"b": 12,"c": 10,"d": 54,"e": 4},"Series_2": {"yes": 20, "no": 16},"Series_3": {"s": 9, "n": 7}},"text": [{"t": "header","c": "text"}, {"t": "header2","c": "text2" }]}]}; 
 
     transpose = (arr) => arr.reduce((r,v,i) => { 
 
     (r[i] = r[i] || []).push(v); 
 
     return r; 
 
     },[]); 
 

 
let example_output = []; 
 

 
data.el1.forEach((o) => { 
 
    
 
    Object.keys(o.data1).reduce((r, k, i) => { 
 
    r[i] = r[i] || {}; 
 

 
    if('labels' in r[i]) 
 
     r[i]['labels'].push(o.date); 
 
    else 
 
     r[i]['labels'] = [o.date]; 
 

 
    if('series' in r[i]) 
 
     Object.values(o.data1[k]).forEach((v,j) => r[i]['series'][j].push(v)); 
 
    else 
 
     r[i]['series'] = transpose(Object.values(o.data1[k])); 
 
    
 
    return r; 
 
    },example_output); 
 
    
 
},[]); 
 

 
console.log(example_output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

感謝您的幫助!你的腳本完美無缺!但是我有一個問題,這個腳本是在ECMA 6中編寫的,而且在IOS <10.0中工作不了,你可以給我一些建議,如何將它轉換爲ECMA 5 – breakbit

+0

用'function'替換箭頭函數('=>') –

相關問題