javascript - how to map many arrays values that have same construct to an object? -
how map nested array's values object template, has same construct? had tried ways, still can't goal, can me favor? original data, got backend!
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 ] ] } ];
target data looks below construct!
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" } } ];
just partly ok. how can using index iterate object?
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; (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 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)`
you iterate , map values assigned types.
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; }
Comments
Post a Comment