In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve judgment in JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Monistic judgment
1.1 for example?
We write a common function of if/else judgment, and then optimize it.
Const myFunction = (status) = > {if (status = 1) {console.log ("status1");} else if (status = 2) {console.log ("status2");} else if (status = 3) {console.log ("status3");}}; 1.2 into Object
We know that the Object of JavaScript is actually an unordered set of key-value pairs, which is why we can use it to store the conditions for judgment. For example, in the above case, the judgment condition is a numeric type, and the subsequent operation only uses a string, so we can create an object and use the number and string used as the key name and corresponding value of the Object, respectively.
/ / put the judgment condition into Object const statusObj = {1: "status1", 2: "status2", 3: "status3",}; / / optimized function? const myFunction = (status) = > {console.log (statusObj [status]);}; 1.3 into Map
In addition to the original object, we can also use the Map object. Let's take a look at how MDN described it:
The Map object holds the key-value pair and remembers the original insertion order of the key. Any value (object or original value) can be used as a key or a value.
It is not difficult to see that the Map object is actually an enhanced version of ordinary objects, especially any value can be used as its key-value pair, which means that functions, regularities, etc., can also be used as their keys or values, which greatly facilitates us to use it as a judgment operation. The details about the Map object are not expanded here.
/ / put the judgment condition into Map const statusMap = new Map ([[1, "status1"], [2, "status2"], [3, "status3"],]); / / optimized function? const myFunction = (status) = > {console.log (statusMap.get (status));}; second, multiple judgment
2.1 for example?
Since monistic judgment can be optimized, so multivariate judgment can also be optimized. here are two cases of judgment conditions.
/ for example? const myFunction = (right, status) = > {if (right = "administrator" & & status = = 1) {console.log ("administrators like Wang Bingbing");} else if (right = "administrator" & & status = = 2) {console.log ("administrators don't like Wang Bingbing");} else if (right = = "user" & & status = = 1) {console.log ("users like Wang Bingbing") } else if (right = = "user" & & status = 2) {console.log ("users don't like Wang Bingbing");}}; / / examples of repetition? const myFunction = (right, status) = > {if (right = "administrator" & & status = = 1) {console.log ("administrators like Wang Bingbing") } else if (right = = "administrator" & & status = 2) {console.log ("administrators like Wang Bingbing");} else if (right = "user" & & status = 1) {console.log ("users like Wang Bingbing");} else if (right = = "user" & & status = = 2) {console.log ("users like Wang Bingbing");}}; 2.2 spell the judgment conditions into strings and put them into Object
Both cases can also be optimized with Object.
/ / optimize "example?" / / put the judgment conditions into Object const actionsObj = {"administrator-1": "administrators like Wang Bingbing", "administrator-2": "administrators do not like Wang Bingbing", "user-1": "users like Wang Bingbing", "user-2": "users do not like Wang Bingbing",} / / optimized function? const myFunction = (right, status) = > {console.log (actionsObj [`${right}-${status}`]);} / / you can use the function as "value". The following situations are similar. Const actionsObj = {"administrator-1": () = > console.log ("administrator likes Wang Bingbing"), "administrator-2": () = > console.log ("administrator likes Wang Bingbing"), "user-1": () = > console.log ("administrator likes Wang Bingbing") "user-2": () = > console.log ("administrators like Wang Bingbing"),} / / optimized function? const myFunction = (right, status) = > {actionsObj [`${right}-${status}`] ();}; / optimize "examples with repetition?" / / Public functions can be extracted. The following situations are similar, not to repeat? const actions = () = > {const F1 = () = > console.log ("administrators like Wang Bingbing"); const f2 = () = > console.log ("users like Wang Bingbing") Return {"administrator-1": F1, "administrator-2": F1, "user-1": f2, "user-2": f2,}; / / optimized function? const myFunction = (right, status) = > {actions () [`${right}-${status}`] ();}; 2.3 put the judgment condition into a string into Map
Similarly, we can also use the Map object. We save the two conditions as strings.
/ / optimize the "example?" / / put the judgment condition into Map const actionsMap = new Map ([['administrator-1',' administrator likes Wang Bingbing'], ['administrator-2',' administrator does not like Wang Bingbing'], ['user-1',' users like Wang Bingbing'], ['user-2',' users do not like Wang Bingbing']) / / optimized function? const myFunction = (right, status) = > {console.log (actionsMap.get (`${right}-${status}`));}; 2.4 put the judgment condition into Object and then into Map
/ / optimize the "example?" / / put the judgment condition into Map const actionsMap = new Map ([{right: 'administrator', status: 1}, () = > console.log (' administrator likes Wang Bingbing')], [{right: 'administrator', status: 2}, () = > console.log (' administrator doesn't like Wang Bingbing')], [{right: 'user', status: 1} () = > console.log ('users like Wang Bingbing')], [{right: 'user', status: 2}, () = > console.log (' users don't like Wang Bingbing')]) / / optimized function? const myFunction = (right, status) = > {const action = [... actionsMap] .filter (([e]) = > (e.right = right & & e.status = status)); action.forEach (([_, index]) = > index.call ());}; 2.5 write the judgment condition as regular and then put it into Map
Regular expressions can be used to deal with relatively complex situations.
/ / optimize "examples with repetition?" / / write the judgment condition as regular and then put it in Map const actions = () = > {const F1 = () = > console.log ('administrator likes Wang Bingbing'); const f2 = () = > console.log ('users like Wang Bingbing') Return new Map ([/ ^ administrator- [1-2] /, F1], [/ ^ user- [1-2] /, f2]);}; / / optimized function? const myFunction = (right, status) = > {const action = [... actions ()] .filter (([e]) = > e.test (`${right}-${status}`)); action.forEach ([_, index]) = > index.call (); This is the end of the article on "how to achieve judgment in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 231
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.