I am working with an input object consisting of two arrays.
payload
columns:["col1", "col2","col3"],
data: ["col1value1","col2value1" , "col1value3"] ,
["col1value2","col2value2" , "col1value2"]
My goal is to convert these two arrays into objects as shown below: output = [];
[
{ col1: 'col1value1', col2: 'col2value1', col3: col1value3},
{ col1: 'col1value2', col2: 'col2value2', col3: col1value2}
]
I have been attempting to achieve this using reduce or other methods, but I am struggling. Currently, I am hard coding the index of columns in the data array. Is there a better and faster way to accomplish this without hard coding the column names?
This is how I am currently approaching it:
const indexofCol1= this.payload.columns.indexOf["col1"]
Then I use that index to map through the data
array to retrieve all the values - which I know is not ideal.