destructuring

Destructuring assignment and variable swapping

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 06:32:04
问题 Javascript allows swapping of variables: var x = 1 var y = 2 [x, y] = [y, x] // y = 1 , x = 2 And destructured assignment: var a, b [a, b] = [1, 2] log(a) // 1 log(b) // 2 When using variable swapping in lieu with destructured assignment, trying to swap variables breaks down: var a, b [a, b] = [1, 2] // a = 1, b = 2 [a, b] = [b, a] // TypeError: Cannot set property '2' of undefined Why is that? 回答1: If you decide to omit semicolons (no judgement, I prefer it that way too), don't forget to

Destructure an array parameter [duplicate]

限于喜欢 提交于 2021-02-04 21:17:31
问题 This question already has an answer here : Syntax for destructuring arrays into function parameters in ES6 (1 answer) Closed 1 year ago . Is it possible to destructure a function parameter? For example, I would want to convert this: Object.entries({}).find(group=> group[0] === "foo" && group[1] === "bar"); To something like this: Object.entries({}).find([0: key, 1: value] => key === "foo" && value === "bar"); or Object.entries({}).find([...key, value] => key === "foo" && value === "bar"); 回答1

Destructure object property AND whole object itself [duplicate]

久未见 提交于 2021-01-28 11:20:51
问题 This question already has answers here : Clean way to keep original variable and destructure at the same time (3 answers) Closed last month . I have an object. I know I can destructure to retrieve the value of any entry, and use spread operator to retrieve the rest of them const [a, ...rest] = [1, 2, 3]; console.log(a); // 1 console.log(rest); // [ 2, 3 ] I would like to know if there is any sintaxis to retrieve both a value of any entry, and the object itself redeclared to a new var,

Does an ES6 shorthand exist for copying a subset of an object's properties into a new object? [duplicate]

旧巷老猫 提交于 2020-12-06 04:21:08
问题 This question already has answers here : One-liner to take some properties from object in ES 6 (11 answers) How to get a subset of a javascript object's properties (27 answers) Closed 4 years ago . Consider the following object: const obj = {a: 1, b: 2, c: 3, d: 4, e: 5} Is there a simple syntax for creating a new object that contains: const obj2 = {a, b, d} I'm aware that underscore & lodash have .pick() , but I'm hoping there's some kind of destructuring trickery that I may not be aware of.

What is Serilog destructuring?

微笑、不失礼 提交于 2020-12-03 06:27:18
问题 What is the purpose of Serilog's @ syntax? If I run the following: var dummy = new { Foo = "Bar", Date = DateTime.Now }; Log.Information("Dummy object: {Dummy}", dummy); Then I get an output to the console like so: Time: 16:20 [Level: Information] (ManagedThreadID: 8) Message: Dummy object: "Foo = Bar, Date = 25/06/2016 16:20:30 }" If I change the {Dummy} to {@Dummy} then I get the same output Time: 16:22 [Level: Information] (ManagedThreadID: 8) Message: Dummy object: Foo: "Bar", Date: 06/25

Destructuring with nested objects and default values

情到浓时终转凉″ 提交于 2020-11-26 12:16:38
问题 I'm using destructuring to declare some variables like this: const { a, b, c } = require('./something'), { e = 'default', f = 'default'} = c; Is there a way to make this into single line? I've tried something like : const { a, b, c = { e = 'default', f = 'default'} } = require('./something'); But it gives me an error: SyntaxError: Invalid shorthand property initializer 回答1: Just replace = with : : const {a, b, c: {e = 'default', f = 'default'}} = require('./something') Demo: const { a, b, c:

Destructuring with nested objects and default values

三世轮回 提交于 2020-11-26 12:01:06
问题 I'm using destructuring to declare some variables like this: const { a, b, c } = require('./something'), { e = 'default', f = 'default'} = c; Is there a way to make this into single line? I've tried something like : const { a, b, c = { e = 'default', f = 'default'} } = require('./something'); But it gives me an error: SyntaxError: Invalid shorthand property initializer 回答1: Just replace = with : : const {a, b, c: {e = 'default', f = 'default'}} = require('./something') Demo: const { a, b, c:

Destructuring with nested objects and default values

拜拜、爱过 提交于 2020-11-26 11:59:37
问题 I'm using destructuring to declare some variables like this: const { a, b, c } = require('./something'), { e = 'default', f = 'default'} = c; Is there a way to make this into single line? I've tried something like : const { a, b, c = { e = 'default', f = 'default'} } = require('./something'); But it gives me an error: SyntaxError: Invalid shorthand property initializer 回答1: Just replace = with : : const {a, b, c: {e = 'default', f = 'default'}} = require('./something') Demo: const { a, b, c:

Destructuring with nested objects and default values

会有一股神秘感。 提交于 2020-11-26 11:56:38
问题 I'm using destructuring to declare some variables like this: const { a, b, c } = require('./something'), { e = 'default', f = 'default'} = c; Is there a way to make this into single line? I've tried something like : const { a, b, c = { e = 'default', f = 'default'} } = require('./something'); But it gives me an error: SyntaxError: Invalid shorthand property initializer 回答1: Just replace = with : : const {a, b, c: {e = 'default', f = 'default'}} = require('./something') Demo: const { a, b, c: