Destructuring

Array Destructuring

Très pratique pour les tests unitaires.
1
const userList = [
2
{firstName: 'Foo'},
3
{firstName: 'John'}
4
];
5
6
const [user1, user2, user3, user4 = null] = userList;
7
8
console.log(user1); // { firstName: 'Foo' }
9
console.log(user2); // { firstName: 'John' }
10
console.log(user3); // undefined
11
console.log(user4); // null
Copied!

Object Destructuring

1
const user = {
2
firstName: 'Foo',
3
lastName: 'BAR',
5
};
6
7
const {firstName, lastName, phoneNumber} = user;
8
9
console.log(firstName); // Foo
10
console.log(lastName); // BAR
11
console.log(phoneNumber); // undefined
Copied!

Dernière mise à jour 4yr ago