I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes
Please refer to the image below
As you can see, on the 2nd record value 1:Three should be placed on col 1
This is my array of objects:
const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]
I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>
So as one of the suggestion, here's my attempt:
const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)
I'm hopping my JSON will shape up like this:
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]
Let me know if anyone can come up with better solution
Thanks in advance!
