简体   繁体   English

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

[英]How to check if array of object is empty?

I want to check if the array of objects is empty or not, I'm storing the result from API call in state city_name .我想检查对象数组是否为空,我将 API 调用的结果存储在状态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)

I keep getting an error double java.lang.Double.doubleValue() on a null object .我不断收到错误double java.lang.Double.doubleValue() on a null object even tried cities.length == 0 and (this.state.city_name).lenght == 0 it return the same error.甚至尝试过cities.length == 0(this.state.city_name).lenght == 0它返回相同的错误。

This is the data from API call:这是来自 API 调用的数据:

city_name = [{"country_flag_url": "https://b.zmtcdn.com/images/countries/flags/country_216.png", "country_id": 216, "country_name": "United States", "discovery_enabled": 1, "has_go_out_tab": 0, "has_new_ad_format": 0, "id": 11282, "is_state": 0, "name": "Del Aire, CA", "should_experiment_with": 0, "state_code": "CA", "state_id": 73, "state_name": "California"}, {"country_flag_url": "https://b.zmtcdn.com/images/countries/flags/country_216.png", "country_id": 216, "country_name": "United States", "discovery_enabled": 0, "has_go_out_tab": 0, "has_new_ad_format": 0, "id": 4463, "is_state": 0, "name": "Del Norte, CO", "should_experiment_with": 0, "state_code": "CO", "state_id": 74, "state_name": "Colorado"}, {"country_flag_url": "https://b.zmtcdn.com/images/countries/flags/country_216.png", "country_id": 216, "country_name": "United States", "discovery_enabled": 0, "has_go_out_tab": 0, "has_new_ad_format": 0, "id": 9431, "is_state": 0, "name": "Del Rio, TX", "should_experiment_with": 0, "state_code": "TX", "state_id": 111, "state_name": "Texas"}]

Please help me请帮我

Check for the length of the array and then try to do your task.检查数组的长度,然后尝试执行您的任务。

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

You can do something like this你可以做这样的事情

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

I think the issue may be that you are using === which is strict and you will get the error on a double element.我认为问题可能在于您使用的是严格的 === 并且您将在双元素上收到错误。 Null is a string and double is a number. Null 是一个字符串,double 是一个数字。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查只包含空对象[{}]的数组 - How to check for array containing only an empty object [{}] 如何检查Javascript对象的值是否为数组而不为空 - How to check if Javascript object value is array and not empty 如何检查 object 中的数组是否为空 JavaScript - How to check if array inside object is empty JavaScript 如何检查数组中的对象是否有空字符串? - How to check if an object in an array has an empty string? “undefined is not an object”,需要检查 allNotes 数组是否为空。 如何? - “undefined is not an object”, need to check if allNotes array is empty. How? 如何同时检查 Object 和其他字段中的数组是否为空? - How to check If array is empty in Object and the other fields at the same time? 如何在Javascript中检查对象数组中的空对象属性 - How to Check for an empty object property in an array of objects in Javascript 如何检查每个数组对象属性是否为空? - How to check any every array object property empty or not? 如何在 javascript 中检查对象的属性数组是否为空 - How to check if array of object's properties are empty in javascript 如何检查javascript变量是否存在,为空,数组,(数组为空),未定义,对象等 - How to check if javascript variable exist, empty, array, (array but empty), undefined, object and so on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM