简体   繁体   中英

How do you make sure he selects one?

I want him to select one instead of all.

if(input == ["1", "2"]) {

}

I expect the output is '1 or 2' but the actual output is '1,2'. He goes only further in the code if it is '1,2' as input.

You can use includes to check if the array contains a certain element

 var input = "2"; if ( ["1", "2"].includes(input) ) { console.log("Included"); } 


Another option is using indexOf . This method returns the first index at which a given element can be found in the array, or -1 if it is not present.

 var input = "2"; if ( ["1", "2"].indexOf(input) !== -1 ) { console.log("Included"); } 

Use index.Of , it returns -1 when this is absent, otherwise it returns the index of the position.

input = ["1", "2"];

if(input.indexOf(you_value) != -1){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM