2

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.

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. even tried cities.length == 0 and (this.state.city_name).lenght == 0 it return the same error.

This is the data from API call:

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

1
  • 2
    An empty array is not considered to be null. An empty array is still an array. Don't use the ternary operator here if you're doing console.log(cities), instead use a simple if statement to check the following: if (cities.length === 0) { ToastAndroid.showWithGravity(...) }. Commented Mar 2, 2020 at 13:32

3 Answers 3

2

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)
       }

Sign up to request clarification or add additional context in comments.

2 Comments

It should be length not lenght.
sorry for the typo, it is length indeed
2

Check for the length of the array and then try to do your task.

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

Comments

0

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.

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

1 Comment

if( cities && cities.length>0 ) to properly handle undefined too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.