i have two different arrays one have keys and one have values i want to combine this two arrays and create new one?
var keys = [
{ keyName: "col1", DispName: "Column 1" },
{ keyName: "col2", DispName: "Column 2" },
];
var values = [
{ col1: "Elijah", col2: "Michealson" },
{ col1: "Jhon", col2: "wick" },
];
var mappedData = new Array(values.length);
mappedData.fill({});
for (let i = 0; i < values.length; i++) {
for (let key of Object.keys(values[i])) {
for (let j = 0; j < keys.length; j++) {
if (key == keys[j]["keyName"]) {
mappedData[i][keys[j]["DispName"]] = values[i][key];
}
}
}
}
console.log(mappedData);
but i'm getting this output
[
{
"Column 1": "Jhon",
"Column 2": "wick"
},
{
"Column 1": "Jhon",
"Column 2": "wick"
}
]
Expected output:
[
{
"Column 1": "Elijah",
"Column 2": "Michealson"
},
{
"Column 1": "Jhon",
"Column 2": "wick"
}
]
what i'm doing wrong here? Thanks in advance