如何检查对象数组是否为空?

时间:2020-03-02 13:28:24

标签: javascript arrays react-native react-native-android

我想检查对象数组是否为空,我将API调用的结果存储为状态 NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); String str = "ch_id_i"; if (VERSION.SDK_INT >= 26) { NotificationChannel notificationChannel = new NotificationChannel(str, "name", NotificationManager.IMPORTANCE_LOW); notificationChannel.enableLights(true); notificationChannel.setLightColor(SupportMenu.CATEGORY_MASK); notificationChannel.enableVibration(false); notificationChannel.setShowBadge(false); notificationmanager.createNotificationChannel(notificationChannel); } builder = new NotificationCompat.Builder(context, str);

city_name

我不断收到错误消息cities = this.state.city_name; console.warn(cities); //this return an empty array [] cities === null ? console.log(cities) : ToastAndroid.showWithGravity('No Result Found',ToastAndroid.LONG) 。 甚至尝试过double java.lang.Double.doubleValue() on a null objectcities.length == 0也会返回相同的错误。

这是来自API调用的数据:

(this.state.city_name).lenght == 0

请帮助我

3 个答案:

答案 0 :(得分:2)

检查数组的长度,然后尝试执行您的任务。

if(cities.length)
// do your task here
else
// handle here

答案 1 :(得分:1)

您可以做这样的事情

 cities = this.state.city_name;
 
 arr = cities.length;
 
 if(arr == 0) {
     console.log('no result found')
       }
 
 else {
     console.log(cities)
       }

答案 2 :(得分:0)

我认为问题可能是您使用的===严格,会在double元素上出现错误。 Null是一个字符串,double是一个数字。

if(cities != null && cities.length>0){
    //not empty, do something with data
}else
    //array is empty, so add values.