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

What is the syntax of Javascript deconstructing assignment

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "what is the grammar of Javascript deconstruction assignment". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the grammar of Javascript deconstruction assignment"?

Fabric deconstruction

Array deconstruction is very simple. All you have to do is declare a variable for each value in the array. You can define fewer variables than the indexes in the array (that is, if you only want to solve the first few values), skip some indexes or even use REST mode to put all the remaining values in the new array.

Const nums = [3,6,9,12,15]; const [k, / k = 31, / / l = 6, / / Skip a value (12)... n / / n = [12,15]] = nums; object deconstruction

Object deconstruction is very similar to array deconstruction, except that each key in the object can be referenced by name, creating a variable with the same name. In addition, you can deconstruct the key to a new variable name, deconstructing only the required key, and then deconstructing the remaining key into new objects using rest mode.

Const obj = {a: 1, b: 2, c: 3, d: 4}; const {a, / a = 1 c: d, / d = 3... rest / / rest = {b: 2, d: 4}} = obj; nesting deconstruction

Nested objects and arrays can be deconstructed by the same rules. The difference is that you can deconstruct a nested key or value directly into a variable without having to store the parent object in the variable itself.

Const nested = {a: {b: 1, c: 2}, d: [1,2]}; const {a: {b: f, / / f = 1. G / / g = {c: 2}},. H / h = {d: [1,2]}} = nested; advanced deconstruction

Because the array behaves like an object, you can get a specific value from the array by using the index as the key in the object deconstruction allocation and using the deconstruction allocation syntax. You can also get other properties of the array (such as the length of the array) in this way. Finally, if the deconstructed value is undefined, you can also define default values for variables during deconstruction.

Const arr = [5, 'baked, 4,' dumped, 'estranged,' faded, 2] Const {6: X, / / x = 20: y, / / y = 52: Z, / / z = 4 length: count, / / count = 7 name = 'array', / / name =' array' (not present in arr). RestData / / restData = {'1percent:' baked, '3percent:' dumped, '4percent:' e' '5percent:'f'}} = arr At this point, I believe you have a deeper understanding of "what is the syntax of Javascript deconstruction assignment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report