js动态为对象添加属性

    //  编辑表格内容
    addItem() {
      const item = {
        editing: true,
        uid: uid++
      }
      // 动态设置对象
      for (let index = 0; index < this.columns.length; index++) {
        var key = this.columns[index].code;
        var value = ""
        item[key] = value;
        if (this.columns[index].dataType === 'Enum') {
          item[key + '_displayname'] = '';
        }
      }
      console.log(item)
      //判断当前是搜索框是不是空   空则直接新增
      if (this.tableList.length > 0) {
        //检查是否有正在编辑的条目  如果没有 则新增一条
        if (this.checkSearchItemByIndex()) {
          this.tableList.push(item)
        }
      } else {
        this.tableList.push(item)
      }
    },

动态设置属性:

      const item = {
        editing: true,
        uid: uid++
      }
      // 动态设置对象
      for (let index = 0; index < this.columns.length; index++) {
        var key = this.columns[index].code;
        var value = ""
        item[key] = value;
        if (this.columns[index].dataType === 'Enum') {
          item[key + '_displayname'] = '';
        }
      }

猜你喜欢

转载自blog.csdn.net/weixin_34285318/article/details/88244278