Home > front end >  How to declared a const inside a map function?
How to declared a const inside a map function?

Time:11-04

I have this computed function linhas() in VueJs, it fills the rows of my DataTable and I need open this function for assigning a value to "this.grupo" for example, so I don't need to use it the way I'm using in "this.maquinas".. And I don't know how to do this. I'm a begginer...

So basically I need a variable inside this function who keeps the group (this.grupo) values. What the best way to do it?

computed: {
    linhas () {
        return this.lista.map(row => ({
            href: { path: this.editRoute   row.id },
            cols: [
                row.id,
                row.tag,
                row.descricao,
                row.tipo === "ANALOGICA" ? "Analógica" : "Digital",
                this.grupo.find(g => g.id === row.id_grupo).nome,
                (this.maquinas.find(m => m.id === this.grupo.find(g => g.id === row.id_grupo).id_maquina) || { nome: "-" }).nome,
                "-"
            ]
        }));
    }
},

CodePudding user response:

Not 100% sure if that's what you're trying but you can simply get the right group once for each row:

//...
linhas() {
    return this.lista.map(row => {
        const group = this.grupo.find(g => g.id === row.id_grupo);
        return ({
            href: {path: this.editRoute   row.id},
            cols: [
                row.id,
                row.tag,
                row.descricao,
                row.tipo === "ANALOGICA" ? "Analógica" : "Digital",
                group.nome,
                (this.maquinas.find(m => m.id === group.id_maquina) || {nome: "-"}).nome,
                "-"
            ]
        });
    });
}
  • Related