01-08-2021 1:01 PM
input
Array1=[ { "col1":"studentname", "col2":"rolno", "col3":"gp_name" }, { "col1":"mohan", "col2":"123", "col3":"maths" }, { "col1":"mahesh", "col2":"1234", "col3":"computer" } ];
expected output
[{ "studentname":"mohan", "rolno":"123", "gp_name":"maths" }, { "studentname":"mahesh", "rolno":"1234", "gp_name":"computer" }]
01-08-2021 1:09 PM
One simple option:
const source = [
{ "col1": "studentname", "col2": "rolno", "col3": "gp_name" },
{ "col1": "mohan", "col2": "123", "col3": "maths" },
{ "col1": "mahesh", "col2": "1234", "col3": "computer" }
];
const header = source.shift();
const headerMap = new Map();
for(var prop in header) {
headerMap.set(prop, header[prop]);
}
const target = source.map((entry) => {
let res = {};
for (var [key, value] of headerMap.entries()) {
res[value] = entry[key];
}
return res;
});
01-08-2021 1:44 PM
thanks
can u help me for dynamic column name also . in real col1,col2....value can be more than 10 sometime 12 how to add the column name dynamically based on the length .
i cannot hardcode the column name directly . .every column name starts with col only . based on the count length it will increase the col1,or col2,col3
01-08-2021 1:54 PM