Last active
February 20, 2024 08:45
-
-
Save Offirmo/f5245b52f7c6ddc88daa6134c3b4e78b to your computer and use it in GitHub Desktop.
[JS deep destructuring] #JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Op%C3%A9rateurs/Affecter_par_d%C3%A9composition | |
var metadata = { | |
title: "Scratchpad", | |
translations: [ | |
{ | |
locale: "de", | |
localization_tags: [ ], | |
last_edit: "2014-04-14T08:43:37", | |
url: "/de/docs/Tools/Scratchpad", | |
title: "JavaScript-Umgebung" | |
} | |
], | |
url: "/en-US/docs/Tools/Scratchpad" | |
}; | |
var { title: englishTitle, translations: [{ title: localeTitle }] } = metadata; | |
console.log(englishTitle); // "Scratchpad" | |
console.log(localeTitle); // "JavaScript-Umgebung" | |
// + in parameters with defaults | |
// https://wesbos.com/destructuring-default-values/ | |
const person = { first: 'Wes', last: 'Bos' }; | |
const { middle: middleName = 'Super Rad' } = person; | |
console.log(middleName); // 'Super Rad' | |
// with dynamic keys | |
> const key = 'a'; | |
undefined | |
> const { [key]: value, ...rest } = { a: 1, b: 2, c: 3 }; | |
undefined | |
> value | |
1 | |
> rest | |
{ b: 2, c: 3 } | |
> const remove = (key, { [key]: _, ...result }) => result; | |
undefined | |
> remove('a', { a: 1, b: 2, c: 3 }); | |
{ b: 2, c: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment