If you have a nested array of rows (array of arrays), use this snippet to match them up with an array of header values.
E.x., say you have an array such as the one below:
const valArr = [
["val1", "val2", "val3"],
["val1", "val2", "val3"],
["val1", "val2", "val3"],
];
And you also have this array of headers:
const headers = [col1, col2, col3];
Use this snippet to join them together:
let returningArray = valArr.map((rowArr, indexOfRowArr) => {
let finalObj = rowArr.reduce(
(accu, currentValueOfRowArr, indexOfCurrentValueOfRowArr) => {
let objToPush = {};
let currentHeader = headers[indexOfCurrentValueOfRowArr];
if (currentHeader in accu) {
console.log("duplicate header");
return accu;
} else {
accu[currentHeader] = currentValueOfRowArr;
}
return accu;
},
{},
);
return finalObj;
});
Your final array should look something like this:
[
{
col1: "val1",
col2: "val2",
col3: "val3",
},
{
col1: "val1",
col2: "val2",
col3: "val3",
},
{
col1: "val1",
col2: "val2",
col3: "val3",
},
];