2017-08-14 93 views
-5

如何將嵌套數組的值映射到具有相同構造的對象模板?我嘗試了一些方法,但仍然無法實現目標,任何人都可以幫我一個忙嗎?這是我從後端獲得的原始數據!如何將具有相同構造的多個數組值映射到對象?

const test_array = [ 
 
    { 
 
     "name": "AnyManagedFundsRow", 
 
     "columnMeta": { 
 
      "a0": "STRING", 
 
      "a1": "STRING", 
 
      "a2": "STRING", 
 
      "a3": "DATE", 
 
      "a4": "DATE", 
 
      "a5": "DOUBLE", 
 
      "a6": "INT" 
 
     }, 
 
     "rows": [ 
 
      [ 
 
       "華夏基金管理有限公司", 
 
       "華夏大中華企業精選靈活配置混合(QDII)", 
 
       "其他型基金", 
 
       "2016-01-20", 
 
       "", 
 
       21.877086009428236, 
 
       65135 
 
      ], 
 
      [ 
 
       "華夏基金管理有限公司", 
 
       "華夏大盤精選混合", 
 
       "混合型基金", 
 
       "2015-09-01", 
 
       "2017-05-02", 
 
       10.307680340705128, 
 
       2944 
 
      ] 
 
     ] 
 
    } 
 
];

目標數據看起來像下面的構造!

let target_data = [ 
 
    { 
 
     "A0": { 
 
      "Description": "華夏基金管理有限公司",, 
 
      "type": "STRING" 
 
     }, 
 
     "A1": { 
 
      "Description": "華夏大中華企業精選靈活配置混合(QDII)", 
 
      "type": "STRING" 
 
     }, 
 
     "A2": { 
 
      "Description": "其他型基金", 
 
      "type": "STRING" 
 
     }, 
 
     "A3": { 
 
      "Description": "2016-01-20", 
 
      "type": "DATE" 
 
     }, 
 
     "A4": { 
 
      "Description": "", 
 
      "type": "DATE" 
 
     }, 
 
     "A5": { 
 
      "Description": "21.877086009428236", 
 
      "type": "DOUBLE" 
 
     }, 
 
     "A6": { 
 
      "Description": "65135", 
 
      "type": "INT" 
 
     } 
 
    }, 
 
    { 
 
     "A0": { 
 
      "Description": "華夏基金管理有限公司",, 
 
      "type": "STRING" 
 
     }, 
 
     "A1": { 
 
      "Description": "華夏大盤精選混合", 
 
      "type": "STRING" 
 
     }, 
 
     "A2": { 
 
      "Description": "混合型基金", 
 
      "type": "STRING" 
 
     }, 
 
     "A3": { 
 
      "Description": "2015-09-01", 
 
      "type": "DATE" 
 
     }, 
 
     "A4": { 
 
      "Description": "2017-05-02", 
 
      "type": "DATE" 
 
     }, 
 
     "A5": { 
 
      "Description": "10.307680340705128", 
 
      "type": "DOUBLE" 
 
     }, 
 
     "A6": { 
 
      "Description": "2944", 
 
      "type": "INT" 
 
     } 
 
    } 
 
];

只是部分確定。我如何使用索引迭代一個對象?

c_obj[index] = value;

const test_array = [ 
 
    { 
 
     "name": "AnyManagedFundsRow", 
 
     "columnMeta": { 
 
      "a0": "STRING", 
 
      "a1": "STRING", 
 
      "a2": "STRING", 
 
      "a3": "DATE", 
 
      "a4": "DATE", 
 
      "a5": "DOUBLE", 
 
      "a6": "INT" 
 
     }, 
 
     "rows": [ 
 
      [ 
 
       "華夏基金管理有限公司", 
 
       "華夏大中華企業精選靈活配置混合(QDII)", 
 
       "其他型基金", 
 
       "2016-01-20", 
 
       "", 
 
       21.877086009428236, 
 
       65135 
 
      ], 
 
      [ 
 
       "華夏基金管理有限公司", 
 
       "華夏大盤精選混合", 
 
       "混合型基金", 
 
       "2015-09-01", 
 
       "2017-05-02", 
 
       10.307680340705128, 
 
       2944 
 
      ] 
 
     ] 
 
    } 
 
]; 
 

 
const tabs_obj = {}; 
 

 
const tabs = test_array.map(
 
    // tab 
 
    (tab, index) => { 
 
     let p_obj = {}, 
 
      c_obj = {}; 
 
     p_obj[tab.name] = []; 
 
     // object keys length 
 
     let key_length = Object.keys(tab.columnMeta).length; 
 
     for (let key in tab.columnMeta) { 
 
      let obj = {}; 
 
      if (tab.columnMeta.hasOwnProperty(key)) { 
 
       obj.type = tab.columnMeta[key]; 
 
       obj.Description = ""; 
 
       c_obj[key.toUpperCase()] = obj; 
 
       // "a0".toUpperCase(); === "A0" 
 
      } 
 
      console.log(`%c tabs${index} & c_obj[key.toUpperCase] = \n`, "color: #f0f", JSON.stringify(c_obj, null, 2)); 
 
      // c_obj = {"A0": ""} 
 
     } 
 
     let t_obj = {}; 
 
     for(let arr of tab.rows){ 
 
      arr.map(
 
       (value, index) => { 
 
        // c_obj[index] = value; 
 
        t_obj[index] = value; 
 
        for(key in c_obj){ 
 
         c_obj[key].Description = value; 
 
        } 
 
       } 
 
      ); 
 
     } 
 
     p_obj[tab.name].push(c_obj); 
 
     console.log("%c \n\n finish a c_obj! = \n\n", "color: red", JSON.stringify(c_obj, null, 4)); 
 
     // c_obj = {"A0": "","A1": "","A2": "",A3: "",A4: "", A5: "", A6: ""} 
 
     return p_obj; 
 
    } 
 
); 
 

 
// format JSON : JSON.stringify(c_obj, null, 4)`

+0

https://github.com/gildata/RAIO/issues/77 – xgqfrms

+1

「_I試過一些方法,_」 那麼告訴我們你嘗試過什麼,解釋什麼都沒有工作,然後問一個具體的問題。你應該閱讀[爲什麼是「有人可以幫我嗎?」不是一個實際的問題?](http://meta.stackoverflow.com/q/284236) – csmckelvey

+0

https://stackoverflow.com/questions/43137010/convert-陣列值到對象的按鍵/ 45687156#45687156 – 2017-08-15 05:54:14

回答

1

你可以迭代和值有指定的類型映射。

var test_array = [{ name: "AnyManagedFundsRow", columnMeta: { a0: "STRING", a1: "STRING", a2: "STRING", a3: "DATE", a4: "DATE", a5: "DOUBLE", a6: "INT" }, rows: [["華夏基金管理有限公司", "華夏大中華企業精選靈活配置混合(QDII)", "其他型基金", "2016-01-20", "", 21.877086009428236, 65135], ["華夏基金管理有限公司", "華夏大盤精選混合", "混合型基金", "2015-09-01", "2017-05-02", 10.307680340705128, 2944]] }], 
 
    target_data = test_array[0].rows.map(a => 
 
     a.reduce((r, c, i) => 
 
      Object.assign(r, { 
 
       ['A' + i]: { Description: c, type: test_array[0].columnMeta['a' + i] } 
 
      }), {})); 
 

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

0

您可以使用array#reduceObject.keys()。對於每個rows,通過遍歷columnMeta來創建一個新對象。

const test_array = [{ name: "AnyManagedFundsRow", columnMeta: { a0: "STRING", a1: "STRING", a2: "STRING", a3: "DATE", a4: "DATE", a5: "DOUBLE", a6: "INT" }, rows: [["華夏基金管理有限公司", "華夏大中華企業精選靈活配置混合(QDII)", "其他型基金", "2016-01-20", "", 21.877086009428236, 65135], ["華夏基金管理有限公司", "華夏大盤精選混合", "混合型基金", "2015-09-01", "2017-05-02", 10.307680340705128, 2944]] }]; 
 

 
var result = test_array[0]['rows'].reduce((a,e) => { 
 
    var obj = Object.keys(test_array[0]['columnMeta']).reduce((obj,key,i) => { 
 
    return Object.assign(obj, { [key.toUpperCase()] : {'Description' : test_array[0]['columnMeta'][key], 'type' : e[i]} }); 
 
    },{}); 
 
    a.push(obj); 
 
    return a; 
 
},[]); 
 
console.log(result);

相關問題