32

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

I have this array 'acessos' that's empty

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

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 Kode
5,5381 gold badge24 silver badges39 bronze badges
asked May 9, 2017 at 11:57
5
  • Do: if(!this.state.acessos.length) { ... } Commented May 9, 2017 at 12:05
  • 1
    @Proz1g it should probably be pointed out that you should not change items in state directly but always use setState instead. The documentation states "Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable." Meaning you should avoid doing things like this.state.acessos.push(...); Technically what you're doing there works fine because you are using setState after, but I wanted to point that out for anyone else happening across this. Commented May 9, 2017 at 16:49
  • @jasonmerino so how can I push an element into the array without using this.state.push? Commented May 9, 2017 at 17:45
  • @Proz1g this.setState({ acessos: [...this.state.acessos, { uuid: ... }] }) would accomplish this. But as I mentioned, what you have works because you are calling this.setState after mutating it. I just wanted to make sure it was clear that mutating items in the state directly and not calling this.setState after would be problematic. Commented May 9, 2017 at 17:57
  • Ah ok! Thank you very much! ;) Commented May 9, 2017 at 18:15

6 Answers 6

53

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
answered May 9, 2017 at 14:29

Comments

32

Just check if your array exists and has a length:

if (this.state.products && this.state.products.length) {
 //your code here
}

No need to check this.state.products.length > 0.
0 is falsy anyway, a small performance improvement.

Ramesh R
7,0874 gold badges27 silver badges40 bronze badges
answered Oct 31, 2018 at 8:48

Comments

7

If checking the array length as part of conditional rendering in React JSX, remember that if condition is falsy at length check, it will render a 0 as in the following example:

{myArray && myArray.length && (
 <div className="myclass">
 Hello
 </div>
 )}

This is not what we want most of the time. Instead you can modify the JSX as follows:

{myArray && myArray.length? (
 <div className="myclass">
 Hello
 </div>
 ):""}
answered Mar 18, 2021 at 6:00

Comments

6

You need not even check for the length since ECMAScript 5.1 You can simply write the same condition as follows.

this.state.acessos && this.state.acessos.length

By default this.state.acessos.length checks if the length is NOT undefined or null or zero.

answered Oct 31, 2018 at 8:43

Comments

0

instead ofnull its better to replace "" like this if ( this.state.acessos == ""){}

answered Mar 29, 2022 at 19:52

2 Comments

Add some comments to your code explaining the why?
acessos is not a string, so why would you compare it to a string? You must provide an informed solution, and not simply an opinion such as "I think that..., it's better to...". I suggest you edit this answer to explain the why, or remove it.
0
 Array?console.log('array is empty'):console.log('array have values')

or

if (Array){
 console.log('array is empty')
else{
 console.log('array have values')
}
answered Mar 16, 2024 at 14:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.