Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use JavaScript", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use JavaScript" this article.

Learn and use skillfully

1. New Set ()

Some people may know that Set, a new data structure, is available in ES6, but not many people can use it flexibly. Using the Set data structure, we can easily repeat an array, such as:

Let arr = [1, 2, 2, 3]; let set = new Set (arr); let newArr = Array.from (set); / / the Array.from method can convert the Set structure to an array. Console.log (newArr); / / [1,2,3]

2. Object.assign ()

Object.assign () is also an extension method for objects provided in ES6, which can be used to merge copies of objects, such as:

Let obj1 = {a: 1}; let obj2 = {b: 2}; let obj3 = Object.assign ({}, obj1, obj2); console.log (obj3); / / {a: 1, b: 2}

3. Map ()

The map method is used to traverse an array and has a return value, which can operate on each item of the array and generate a new array, sometimes instead of for and forEach loops, simplifying the code, such as:

Let arr3 = [1,2,3,4,5]; let newArr3 = arr3.map ((e, I) = > e * 10); / / multiply each term in the array by 10console.log (newArr3); / / [10,20,30,40,50]

4. Filter ()

The filter method is also used to traverse an array, which, as the name implies, filters the array, triggers a callback function after each element, determines, retains or removes the current item, and finally returns a new array, such as:

Let arr4 = [1,2,3,4,5]; let newArr4 = arr4.filter ((e, I) = > e% 2 = 0); / / take the module, the number console.log (newArr4) whose filter remainder is not zero; / / [2J4]

5. Some ()

The some method is used to traverse the array, triggering a callback function after each element, and returns true as long as one satisfies the condition. Otherwise, it returns false, similar to | | compare, for example:

Let arr5 = [{result: true}, {result: false}]; let newArr5 = arr5.some ((e, I) = > e.result); / / if one is true, it is trueconsole.log (newArr5); / / true

6.every ()

The every method is used to traverse the array, triggering a callback function after each element, and returns false as long as one does not meet the condition, otherwise it returns true, similar to & & comparison, such as:

Let arr6 = [{result: true}, {result: false}]; let newArr6 = arr6.every ((e, I) = > e.result); / / if one is false, it is falseconsole.log (newArr6); / / false

7. ~ operator

~ symbol used in JavaScript has the function of inversion by bit, that is, it is inverted twice, and the operation value of bit operation is required to be an integer, and the result is also an integer, so after bit operation, it will automatically become an integer, which can skillfully remove the decimal part, similar to parseInt, such as:

Let a = 1.23 setlet b =-1.23 Shi console.log (~ a); / / 1console.log (~ b); / /-1

8. | | operator

Ingenious use of the | | operator we can set default values for variables, such as:

Let c = 1 d = c | | 2; / / if the value of c is true, the existing value is taken, otherwise it is 2console.log (d); / / 1

9.... Operator

... Operator is a method used to deconstruct an array in ES6 and can be used to quickly get the parameters of an array, such as:

Let [num1,... nums] = [1,2,3]; console.log (num1); / / 1console.log (nums); / / [2,3]

10. Ternary operator

This operator should be familiar to everyone, and can simplify the writing of if else in the case of dictation, such as:

Let e = true, f =''; if (e) {f = 'man';} else {f =' woman';} / / equivalent to e? F = 'man': F =' woman'; above is all the content of this article "how to use JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report