Timeline for How to get a subset of a javascript object's properties
Current License: CC BY-SA 4.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 26, 2023 at 20:13 | history | edited | Ry-♦ | CC BY-SA 4.0 |
grammar, formatting
|
| Aug 4, 2020 at 11:47 | history | rollback | Nick Parsons |
Rollback to Revision 4
|
|
| May 12, 2020 at 8:17 | comment | added | Estus Flask |
A correct way to do this is to declare temp vars, let a, c; const picked = ({a,c} = object, {a,c}). Unfortunately, comma operator wasn't suggested in other answers for a very good reason, it doesn't make this any easier than const {a, c} = object; const picked = {a,c}.
|
|
| Feb 13, 2020 at 11:16 | comment | added | Josh from Qaribou | The namespace pollution makes this completely impractical. It's extremely common to already have variables in scope that match object properties, that's why the prop shorthand + destructuring exists.Very likely you'll have height or color already defined like in the original example. | |
| Feb 9, 2020 at 5:04 | history | edited | Roman | CC BY-SA 4.0 |
deleted 116 characters in body
|
| Feb 9, 2020 at 4:58 | history | edited | Roman | CC BY-SA 4.0 |
Enhanced variant
|
| Aug 15, 2019 at 12:12 | comment | added | mindplay.dk |
Note that this approach pollutes the current scope with two variables a and c - be careful not to randomly overwrite local or global vars depending on the context. (The accepted answer avoids this issue by using two local variables in an inline function, which falls out of scope after immediate execution.)
|
|
| Jul 18, 2019 at 6:55 | comment | added | kimbaudi |
this solution is clever, but it doesn't work in strict mode (i.e., 'use strict'). I get a ReferenceError: a is not defined.
|
|
| Jul 13, 2019 at 2:45 | history | edited | Code Maniac | CC BY-SA 4.0 |
removed extra characters
|
| May 28, 2019 at 14:48 | history | edited | greuze | CC BY-SA 4.0 |
added 1 character in body
|
| Feb 10, 2019 at 3:38 | history | edited | Code Maniac | CC BY-SA 4.0 |
added 2 characters in body
|
| Feb 10, 2019 at 3:17 | history | answered | Code Maniac | CC BY-SA 4.0 |