1

I have an array

const myArray = this.props.values.students;

How can I display none if the array is empty?

This is what I'm currently using...

<p>{this.props.values.students ? myArray : 'None' }</p>

It doesn't seem to render 'None' if the array is in-fact empty. How can I make this work?

1
  • 2
    Check the length of the array? {this.props.values.students.length ? ... : 'None'} Commented May 16, 2016 at 16:21

1 Answer 1

10

The problem is that an empty array is not a falsy value:

if ([]) {
  console.log('truly - this will happen');
}
else {
  console.log('false - this will *never* happen');
}

You can however check the length of the array, which will give a falsy value when empty (0)

<p>{this.props.values.students.length ? myArray : 'None' }</p>
Sign up to request clarification or add additional context in comments.

1 Comment

Very clear explanation! Did you mean "truthy" instead of "truly"? Also, might be worth using myArray.length at that point, for further clarity/brevity.

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.