Looking to insert data from four array values into an array of objects in JavaScript?
// Necessary input
columnHeaders=['deviceName','Expected','Actual','Lost']
machine=['machine 1','machine 2','machine 3','machine 4','machine 5']
data=[
{expectedData:[600,400,500,200,100],label:'Expected'},
{actualData:[500,300,400,100,50],label:'Actual'},
{LostData:[100,100,100,100,50],label:'Lost'}
]
// Desired output
tableData=[
{deviceName:"machine 1",Expected:600,Actual:500,Lost:100},
{deviceName:"machine 2",Expected:400,Actual:300,Lost:100},
{deviceName:"machine 3",Expected:500,Actual:400,Lost:100},
{deviceName:"machine 4",Expected:200,Actual:100,Lost:100},
{deviceName:"machine 5",Expected:100,Actual:50,Lost:50} ]
I attempted to use hardcoded keys, but seeking a more generic solution. Can anyone assist with this problem?
//Attempted solution
let array=[];let obj={};
for(let i=0;i<machine.length;i++){
var expected = data[0].expectedData[i];
var actual = data[1].actualData[i];
var lost = data[2].LostData[i];
obj ={[columnHeaders[0]]:machine[i],[columnHeaders[1]]:expected,[columnHeaders[2]]:actual,[columnHeaders[3]]:lost}
console.log(obj);
array.push(obj);
}
console.log(array);