检查React Native中的数组是否为空 [英] Check if an array is empty in React Native

查看:98
本文介绍了检查React Native中的数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过IF语句检查数组是否为空?

How can I check if an array is empty with a IF statment?

我的数组'acessos'是空的

I have this array 'acessos' that's empty

...
constructor(props){
    super(props);
    this.state = {
      acessos:[]
    };
  }
...

然后,我尝试检查'acessos'是否为空,以及是否将其中的一些数据压入其中.我尝试使用null但没有结果,那么如何检查是否为空?

Then I'm trying to check if 'acessos' is empty and if it is I push some data in it. I've tried with null but with no results, so how can I check if is empty?

...
if(this.state.acessos === null){
      this.state.acessos.push({'uuid': beacons.uuid, 'date':date});
      this.setState({acessos: this.state.acessos});
} else {
...

推荐答案

我同意Julien.另外,您不必将其与null进行比较.你可以这样写

I agree to Julien. Also you don't have to compare it to null. You can write it like

this.state.acessos && this.state.acessos.length > 0

这篇关于检查React Native中的数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆