【问题标题】:How to declared a const inside a map function?如何在 map 函数中声明 const?
【发布时间】:2021-12-17 23:33:40
【问题描述】:

我在 VueJs 中有这个计算函数 linhas(),它填充了我的 DataTable 的行,我需要打开这个函数来为“this.grupo”分配一个值,所以我不不需要像我在“this.maquinas”中使用的那样使用它。我不知道该怎么做。我是一个初学者...

所以基本上我需要在这个函数中使用一个变量来保存组 (this.grupo) 的值。最好的方法是什么?

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,
                "-"
            ]
        }));
    }
},

【问题讨论】:

  • 您能再详细说明一下吗?无意冒犯,但我不明白你在做什么:)
  • @eol Hi :) 我只想在我的函数中添加来自“this.grupo”的值,而不是为了同一个目标多次调用
  • 好的,我想我现在明白了,检查我的答案:)

标签: javascript node.js arrays vue.js


【解决方案1】:

不是 100% 确定这是否是您正在尝试的,但您可以简单地为每一行获取正确的 group

//...
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,
                "-"
            ]
        });
    });
}

【讨论】:

  • 这太棒了!非常感谢,正是这个:)
猜你喜欢
  • 2013-03-13
  • 2011-01-13
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多