javascript destruction
1. Object destruction
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const response = { code: 200, status: 'success', data: { name: 'zdd', age: 18 } }
const { code, status, data: { name, age } } = response; console.log(code); console.log(status); console.log(name); console.log(age);
|
2. String destruction
1 2 3
| const [a, b, c, d, e] = 'hello'; console.log(a); console.log(b);
|
3. rename on destruction
1 2 3 4 5 6
| const person = { name: 'zdd', age: 18 } const { name: myName } = person; console.log(myName);
|
4. default value on destruction
1 2 3 4 5 6 7 8 9
| const person = { name: 'zdd', age: 18, };
const { name, age = 20, gender = 'male' } = person; console.log(name); console.log(age); console.log(gender);
|
Note: Each destructed property can have a default value if and only if the property is not present or its value is undefined
.
1 2 3 4 5 6 7 8 9 10
| const person = { name: 'zdd', age: 18, address: null };
const { name, address = 'Dalian', gender = 'male' } = person; console.log(name); console.log(address); console.log(gender);
|
In the above code, address
is present in the person
object, but its value is null
, so the default value Dalian
is not used.
如果解构中的属性值为null
,则无法进行嵌套解构,会报错。
1 2 3 4 5 6 7 8 9
| const person = { name: 'zdd', address: null };
const { name, address: { city = 'Dalian', street = 'Jiefang Road' } = {} } = person; console.log(name); console.log(city); console.log(street);
|
注意,以上代码会报错,虽然我们给了address
一个默认值{}
,但是address
的原始值是null
,所以默认值无法生效,这导致了city
和street
的解构报错。(相当于在null
值中读取city
和street
)
要想解决这个问题,只能将嵌套解构变为二次解构。
1 2 3 4 5 6 7 8 9 10
| const person = { name: 'zdd', address: null };
const { name, address = {} } = person; const { city = 'Dalian', street = 'Jiefang Road' } = address || {}; console.log(name); console.log(city); console.log(street);
|
第一次解构后,address
的值为null
,所以我们给address
一个默认值{}
,然后再次解构city
和street
,这样就不会报错了。
5. rename and default value at the same time
1 2 3 4 5 6 7 8 9
| const person = { name: 'zdd', age: 18, };
const { name: myName, age: myAge = 20, gender: myGender = 'male'} = person; console.log(myName); console.log(myAge); console.log(myGender);
|
6. Array destruction
1 2 3 4 5
| const nums = [1, 2, 3]; const [a, b, c] = nums; console.log(a); console.log(b); console.log(c);
|
数组解构与对象解构不同,不需要指定属性名,只需要按照顺序解构即可。
1 2 3 4
| const colors = ['red', 'green', 'blue']; const [firstColor, secondColor] = colors; console.log(firstColor); console.log(secondColor);
|
7. get element at specific position
The following code get the color at the third position, ignore the first two colors.
1 2 3
| const colors = ['red', 'green', 'blue']; const [, , thirdColor] = colors; console.log(thirdColor);
|
8. ignore some elements
Get all elements in array except the first two.
1 2 3
| const colors = ['red', 'green', 'blue', 'yellow']; const [, , ...restColors] = colors; console.log(restColors);
|
注意:数组解构中,不定元素必须放在最后,如果不定元素后面还有其他元素,会报错。
1 2
| const colors = ['red', 'green', 'blue', 'yellow']; const [...restColors, lastColor] = colors;
|
9. Copy array
ES5
1 2 3
| const colors = ['red', 'green', 'blue']; const newColors = colors.slice(); console.log(newColors);
|
ES6
1 2 3
| const colors = ['red', 'green', 'blue']; const [...newColors] = colors; console.log(newColors);
|
9. swap two variables
Before ES6, we must introduce a temporary variable to swap two variables. But with destruction, we can swap two variables easily.
1 2 3 4 5
| let a = 1; let b = 2; [a, b] = [b, a]; console.log(a); console.log(b);
|
10. Nested array destruction
1 2 3 4 5 6
| const nums = [1, [2, 3], 4]; const [a, [b, c], d] = nums; console.log(a); console.log(b); console.log(c); console.log(d);
|