js中in的使用

开始判断是否符合条件时太low 然后去搜了一下有什么简便的方法去判断对象是否存在某个属性 

if (this.showTableName === 'ldzy' ||
          this.showTableName === 'gcleq' ||
          this.showTableName === 'erdiao' ||
          this.showTableName === 'ldbh_2011' ||
          this.showTableName === 'lgsj' ||
          this.showTableName === 'ldzy_2019' ||
          this.showTableName === 'gsmm' ||
          this.showTableName === 'tghl' ||
          this.showTableName === 'smhzl_2021' ||
          this.showTableName === 'dsql' ||
          this.showTableName === 'shidimian' ||
          this.showTableName === 'shidixian') {
        console.log(this.form)
        this.EchartsName = this.ChinaName[this.showTableName]
        this.getEchartsData()
        this.innerVisible = true
      } else { // 无图表
        this.$message.warning('当前数据无统计图表')
      }

然后发现in是可以符合的(chinaName就是包含上述那些属性的对象)

 if (this.showTableName in this.ChinaName) {
        console.log(this.form)
        this.EchartsName = this.ChinaName[this.showTableName]
        this.getEchartsData()
        this.innerVisible = true
      } else { // 无图表
        this.$message.warning('当前数据无统计图表')
      }

下面是关于in运算符的详细介绍 

1、简介:如果指定的属性在指定的对象或其原型链中,则 in 运算符返回 true

2、in 右操作数必须是一个对象值。例如,你可以指定使用 String 构造函数创建的字符串,但不能指定字符串文字。 

3、特别是原型链上 如果一个属性是从原型链上继承的 也会返回true

"toString" in {}; // 返回true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值