【问题标题】:vuejs: how to pass the value to data as Array?vuejs:如何将值作为数组传递给数据?
【发布时间】:2019-09-21 12:36:19
【问题描述】:

我将对象的值作为道具传递到表单内的“数据”studentId

data() {
    return {
      form: new Form({
        studentId: []
      })
    };
  },

在我的method 中,classlists 有初始值,即学生的id

insertScore() {
      let students = this.classlists;   //<---this is my props
      let element = [];
      let s = [];
      for (let index = 0; index < students.length; index++) {
        element = students[index].id; 
      }
    }

        //id: 7
        //id:29
        //id:30

我需要使我的 classlists ids 成为一个数组并将其存储到我的 studentId 中,例如 studentId = [7,29,30] ,但是当我 console.log 时我得到的是单独的 Ids

道具:

【问题讨论】:

    标签: arrays vue.js push


    【解决方案1】:

    我不确定这是否是您想要实现的目标,但如果您想要仅具有来自 classlist 的特定属性的新数组,这应该可以工作。

    insertScore() {
        const elements = this.classlists.map(e=>e.id)
    }
    

    【讨论】:

    • 是的,这成功了。 elements 作为数组返回,但我还需要将其传递给我的 studentId
    • const elements = this.classlists.map(e =&gt; (this.form.studentId = e.id)); 我试过了,但我得到的只是最后一个值
    • 只需在this.form.studentId = elements底部insertScore内添加一行
    • 哦,我已经得到了 studentId const elements = this.classlists.map(e =&gt; e.id); this.form.studentId = elements;。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2018-09-12
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    相关资源
    最近更新 更多