gpt4 book ai didi

javascript - 如果有条件地检查 react jsx 中的空值 [es6]

转载 作者:行者123 更新时间:2023-11-30 07:13:52 25 4
gpt4 key购买 nike

我有一个数组

const myArray = this.props.values.students;

如果数组为空,如何显示none?

这是我目前正在使用的...

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

如果数组实际上是空的,它似乎不会呈现“无”。我怎样才能使它工作?

最佳答案

问题是空数组不是假值:

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

但是您可以检查数组的长度,当数组为空时会给出一个假值 (0)

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

关于javascript - 如果有条件地检查 react jsx 中的空值 [es6],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com