问题
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);
}
example of what it prints: nullAC
回答1:
The solution to this was posted in the comments by Yury Tarabanko.
var filter = ''
For good practice console.log would also not be in that position within the code.
来源:https://stackoverflow.com/questions/53263601/can-i-remove-null-from-my-drop-down-selection-variable