0

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.

  • 1
    It looks like you just want to copy all props from `r.data[0]` to `ambalareProps`. Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – Moritz Ringler Jun 13 '23 at 11:40
  • hi @MoritzRingler . it's a step in the right direction, but still incomplete (although it is my fault for not mentioning this). there are some cases where i don't just take props from r.data[0], but also from r.data[1], r.data[2] and so on (as in, not just the first row, but multiple rows). and from what i know about merge, if there are 2 props with the same name, it overwrites the first value with the second value, and i need to keep all values. – Roman Claudiu Jun 13 '23 at 12:17
  • 1
    Please add all the rules you need for merging to your question, or clarify what answer you are looking for. "Sometimes I want A and sometimes B" can only be answered with "Then take A or sometimes B". Or are you just looking for [`Object.entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)? – Moritz Ringler Jun 13 '23 at 12:53
  • @MoritzRingler that's exactly what i was looking for: Object.entries(). I was looking for it directly in the array; didn't think to look for it in the main Object class. Thanks a whole lot. And very sorry for the confusion. – Roman Claudiu Jun 13 '23 at 15:32

0 Answers0