I have an array with labels
columns = ["col1", "col2", "col3", "col4"]
and thousands of arrays with values
values = [
['value1','value2','value3','value4'],
['value1','value2','value3','value4'],
['value1','value2','value3','value4'],
// ...
]
I want to iterate the values and create a single array with this structure:
result = [
{ col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
{ col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
{ col1: "val1", col2: "val2", col3: "val3", col3: "val4" },
]
I tried to iterate both and return the object dynamically, but without success:
const columns = ["col1", "col2", "col3", "col4"];
const values = [
['value1','value2','value3','value4'],
['value1','value2','value3','value4'],
['value1','value2','value3','value4'],
];
const lastArr = values.map((e) =>
columns.map((col) => {
return { col: e[0] };
})
);
console.log(lastArr);
// or manually
const raw = values.map(e =>
columns.map(col => {
return {
col1: e[0],
col2: e[1],
col3: e[2],
col4: e[3],
}
})
);
console.log(raw);
Neither attempts return the desired result.