Determine whether the data in the object is empty Object.keys(xxxxx)

Require

If the data in the object is empty, a pop-up box will be displayed to add data. If data exists, it will prompt "No need to add data if it exists."

demo
<!-- 按钮 -->
    <div class="btn">
      <sec-button
        type="primary"
        plain
        :disabled="disabled"
        @click="add" -------给按钮一个事件---------
      >新增</sec-button>
    </div>
script
//  点击
    add() {
    
    
      -------------判断对象的方法  Object.keys(xxxxx).length === 0----------------------
      if (Object.keys(this.tableData).length === 0) {
    
    
        this.title = '添加'
        this.dialogVisible = true
        this.form = {
    
    
          accessKey: '',
          encryptSecurity: '',
          url: ''
        }
      } else {
    
    
        this.disabled = true
        this.$message('已有数据,无需重新添加 ')
        console.log(this.tableData, 'tableData')
      }
    },

There are two methods in vue to determine whether the data in the object is empty.

  1. JSON.stringify(evtValue)=='{}'
  2. Object.keys(xxx).length==0
    Several ways to judge whether an object is an empty object in js
    1.将json对象转化为json字符串,再判断该字符串是否为"{}" var data = {}; var b (JSON.stringify(data) == "{}"); alert(b);//true
    2. for in loop judgment

Guess you like

Origin blog.csdn.net/GikQxx21_wen/article/details/128804254