Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

javascript - returning true if a condition is met

I have to color a div based on the file extension. However, sometimes the file extension may not match with any of the file extensions that i have mentioned below so in that case I will color the div white. But with my condition below, it's always white even if the file extension matches with 'docx', 'doc', 'txt' what am I doing wrong here?

if(!this.fileExtension.toLowerCase().match('docx', 'doc', 'txt')) return ''

question from:https://stackoverflow.com/questions/65917072/returning-true-if-a-condition-is-met

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the example, you are only matching with the 'docx' extension, maybe encapsulating your code in a for loop that checks for all needed examples can be a solution:

let found = False;
for (ext in ["docx", "doc", "txt"]) {
     if (this.fileExtension.toLowerCase().match(ext)) {
         found = True;
         break;
     }
}
// work your magic with the found variable...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...