我可以从下拉选择变量中删除“null"吗 [英] Can I remove 'null' from my drop down selection variable
问题描述
我有一个多选下拉菜单,允许我的用户使用 react-select 选择多个 A、B 和/或 C 选项.每次选择一个时,我都希望将其添加到选择"变量中以进行记录.我有这个工作,但因为我必须先声明我的变量,它在变量中保存了 'null'.我该如何摆脱这个?代码如下.
I have a multi select drop down allowing my user to select multiple A, B and/or C options, using react-select. Each time one is selected, I want it to be added to a 'choice' variable to keep record. I have this working, but since I have to declare my variable first, it is holding 'null' inside the variable. How do I get rid of this? Code below.
handleChange = (selectedChoice) => {
this.setState({ selectedChoice });
for (var i =0; i < selectedChoice.length; i++) {
var filter = null;
filter +=(selectedChoice[i].value); //removing the plus just resets it to last chosen option
}
console.log(filter);
}
打印内容示例:nullAC
example of what it prints: nullAC
推荐答案
此问题的解决方案由 Yury 在评论中发布塔拉班科.
The solution to this was posted in the comments by Yury Tarabanko.
var filter = ''
为了良好的实践,console.log 也不会在代码中的那个位置.
For good practice console.log would also not be in that position within the code.
这篇关于我可以从下拉选择变量中删除“null"吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!