I have a problem in Vue.JS where I get some table data from a database using Axios. The problem is that the table structure changes based on some user inputs. So, for example if the user checks a checkbox, then the stored procedure return table A (which has the columns A, B, C), but if they leave it unchecked, it returns table B (which has the columns D, E, F). If I know the columns' names, I can do something like
async getAmbalariForProduct(productId) {
let ambalareProps = this.header.model;
const r = await axios.get(`../../api/products/GetProdusAmbalare/${productId}`);
if (r.data.length >= 1) {
ambalareProps.idTipAmbalare1 = r.data[0].idTipAmbalare1;
ambalareProps.idTipAmbalare2 = r.data[0].idTipAmbalare2;
ambalareProps.cantitate1 = r.data[0].cantitate1;
ambalareProps.cantitate2 = r.data[0].cantitate2;
ambalareProps.greutate1 = r.data[0].greutate1;
ambalareProps.greutate2 = r.data[0].greutate2;
ambalareProps.idAmbalare1 = r.data[0].idAmbalare;
}
},
But since the columns change, this isn't a viable option.
In C# I would do something like
async getAmbalariForProduct(productId) {
let ambalareProps = this.header.model;
const r = await axios.get(`../../api/products/GetProdusAmbalare/${productId}`);
if (r.data[0].length >= 1) {
for(int i = 0; i < r.data[0].length; i++)
{
ambalareProps[i] = r.data[0][i];
}
}
},
but of course, that doesn't work, as r.data[0] doesn't have any iterable elements.
Any idea is very much appreciated. Thanks a lot.
Edit: as @moritzringler suggested, merge copies props from one array to another. But in some cases I need to copy multiple rows (r.data[1], r.data[2], so on), and merge overwrite the first row with the second row.