How to make if judgment in js as smooth as silk

How to make if judgment in js as smooth as silk

Preface

A small requirement point in the project, click a button to verify dozens of condition boxes, determine whether all condition boxes have been filled (selected) with data (at least one condition is true) and then perform the corresponding operation

The judgment condition box includes Radio single-select box, Checkbox multiple-select box, Input input box, InputNumber counter, Select selector, Switch switch, etc.

The project uses the Element component library V2.15.6

Data types and default values ​​corresponding to different conditions

  • Radio radio button string ''
  • Checkbox multiple selection box array []
  • Input input box string ''
  • InputNumber counter number 0
  • Select
    • Single choice string ''
    • Multiple selection array []
  • Switch switch boolean false

Code Implementation

Idea 1

Use if to judge directly, and then the code is as follows (the variable is a simulation variable)

// Multi-condition judgment begins, as follows if (obj.radio1 || obj.checkbox1.length > 0 || obj.input1 || obj.inputNumber1 > 0 || obj.select1 || obj.select2.length > 0 || obj.switch1 || obj.radio2 || obj.checkbox2.length > 0 || obj.input2 || obj.inputNumber2 > 0 || obj.select3 || obj.select4.length > 0 || obj.switch2 ...) {
  // do something
} else {
  // If the condition is not met, prompt this.$message({
    message: 'Please select the conditions and try again',
    type: 'warning'
  })
  return false
} 

In actual project scenarios, variable names have many semantic characters. If you only write a few ifs, you will end up writing a long string. Then you can’t write anymore after writing a few (it feels like you are writing a bunch of shit).

Can it be achieved in a more elegant way?

Idea 2

Put the variables to be judged into an array, use map to process them into Boolean type, and use includes to determine whether the array contains the specified Boolean value.

//Multi-condition judgment begins, as follows const arr = [
  obj.radio1,
  obj.checkbox1.length,
  obj.input1,
  obj.inputNumber1,
  obj.select1,
  obj.select2.length,
  obj.switch1,
  obj.radio2,
  obj.checkbox2.length,
  obj.input2,
  obj.inputNumber2,
  obj.select3,
  obj.select4.length,
  obj.switch2 
  ...
]

const arr1 = arr.map(item => Boolean(item))
if (arr1.includes(true)) {
  // do something
} else {
  // If the condition is not met, prompt this.$message({
    message: 'Please select the conditions and try again',
    type: 'warning'
  })
  return false
} 

Well, if we use this method to handle a large number of judgments, it will be smoother. ^-^

Summarize

This is the end of this article on how to make the if judgment in js as smooth as silk. For more relevant content about if judgment in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

Reference Documentation

  • developer.mozilla.org/zh-CN/docs/…
  • developer.mozilla.org/zh-CN/docs/…
  • developer.mozilla.org/zh-CN/docs/…
You may also be interested in:
  • Summary of how JS operates on pages inside and outside Iframe
  • Implementation of a simplified version of JSON.stringify and its six major features explained in detail
  • Summary of various uses of JSON.stringify
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • Summary of JavaScript JSON.stringify() usage
  • Detailed explanation of how to solve the circular reference problem encountered when using JSON.stringify
  • The difference and use of json.stringify() and json.parse()
  • Selenium+BeautifulSoup+json gets the json data in the Script tag
  • About if contains comma expression in JavaScript

<<:  HTML form tag tutorial (1):

>>:  Docker container time zone adjustment operation

Recommend

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

2017 latest version of windows installation mysql tutorial

1. First, download the latest version of MySQL fr...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

Summary of basic knowledge and operations of MySQL database

This article uses examples to explain the basic k...

jQuery treeview tree structure application

This article example shares the application code ...

What should I do if I can't view the source file of a web page?

Q: Whether using Outlook or IE, when you right-cl...

Use CSS variables to achieve cool and amazing floating effects

Recently, I found a fun hover animation from the ...

HTML table mouse drag sorting function

Effect picture: 1. Import files <script src=&q...

Solution to high CPU usage of Tomcat process

Table of contents Case Context switching overhead...

Linux disk space release problem summary

The /partition utilization of a server in IDC is ...

HTML Tutorial: HTML horizontal line segment

<br />This tag can display a horizontal line...

An analysis of div+float, a very important concept in website design

In website construction, you will always encounter...

User-centered design

I've been asked a lot lately about an apparen...