xgqfrms xgqfrms -3 years ago 78
Javascript Question

how to map many arrays values that have same construct to an object?

how to map a nested array's values to an object template, which has the same construct? I had tried some ways, but still can't get the goal, anybody can do me a favor? this is the original data, which I got from 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 like the 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 I using index iterate an 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) => {
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 ${c_obj[key.toUpperCase()]}`, "color: #f0f", c_obj);
// 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 finish a c_obj!", "color: red", c_obj);
// c_obj = {"A0": "","A1": "","A2": "",A3: "",A4: "", A5: "", A6: ""}
return p_obj;
//
}
);




Answer Source

here is a good solution!

/*

Array to Object

*/




// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]

const keys_array = [
    "Location",
    "Passes",
    "Passes%",
    "No ID",
    "No ID%", 
    "Fails",
    "Fails%",
    "Fail Dangerous",
    "Fail Dangerous%",
    "Total"
];

let temp_obj = {};

keys_array.map(
    (value, index) => {
        temp_obj[`${value}`] = "";
        // temp_obj[value] = "";
        return temp_obj;
    }
);

console.log(`temp_obj = `, temp_obj);
// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};



for(let k in temp_obj) {
    console.log(`typeof (k) = `, typeof (k));
    // typeof (k) = string
}




/*

Object to Array

*/


const meaningful_objs = {
    "Location": "",
    "Passes": "",
    "Passes%": "",
    "No ID": "",
    "No ID%": "", 
    "Fails": "",
    "Fails%": "",
    "Fail Dangerous": "",
    "Fail Dangerous%": "",
    "Total": ""
};

// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};




let keys_array = Object.keys(meaningful_objs);

console.log(`keys_array = \n`, keys_array);

// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]






/*

result 

*/



let keys = keys_array;
// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]


let arrays = [
    [
        "Sometown",
        "8,25",
        "53.40%",
        "140",
        "0.90%",
        "7,17",
        "45.40%",
        "48",
        "0.30%",
        "16,30"
    ],
    [
        "Some Other Town",
        "12,78",
        "44.80%",
        "193",
        "0.70%",
        "15,22",
        "54.10%",
        "103",
        "0.40%",
        "28,96"
    ]
];

let result = arrays.map(
    (array) => {
        let temp = {};
        keys.forEach(
            (key, index) => {
                console.log(`key = `, key);
                console.log(`index = `, index);
                temp[`${key}`] = array[index];
            }
        );
        console.log(`temp = `, temp);
        return temp;
    }
);
console.log(`result = `, result);



/*

test

*/


let string_objs = JSON.stringify(result);

// "[{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"},{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}]"


let string_obj1 = JSON.stringify(result[0]);

// "{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"}"


/*


    {
        "Location":"Sometown",
        "Passes":"8,25",
        "Passes%":"53.40%",
        "No ID":"140",
        "No ID%":"0.90%",
        "Fails":"7,17",
        "Fails%":"45.40%",
        "Fail Dangerous":"48",
        "Fail Dangerous%":"0.30%",
        "Total":"16,30"
    }

*/


let string_obj2 = JSON.stringify(result[1]);


// "{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}"


/*

    {
        "Location":"Some Other Town",
        "Passes":"12,78",
        "Passes%":"44.80%",
        "No ID":"193",
        "No ID%":"0.70%",
        "Fails":"15,22",
        "Fails%":"54.10%",
        "Fail Dangerous":"103",
        "Fail Dangerous%":"0.40%",
        "Total":"28,96"
    }


*/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download