Last active
January 25, 2019 14:33
-
-
Save diogobaltazar/a10d9041f1e1f5ab63743febcc665b1d to your computer and use it in GitHub Desktop.
objects | create | clone | get attributes subset | destructuring
This file contains hidden or 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
> [{a:1}, {b:2}, {c:3}].reduce((acc, o) => {return Object.assign(acc, o)}, {}) | |
{a: 1, b: 2, c: 3} | |
> e = {a: 1} | |
> e = {...e, b: 1} | |
{a: 1, b: 2} | |
// Creating object attributes with names of array elements | |
> arr = ['a', 'b', 'c'] | |
> Object.assign(...arr.map((attr, index) => {return {[attr]: index}}), {}) | |
{a: 0, b: 1, c: 2} | |
// Destructuring assignement: | |
// Unpack values from arrays, or properties from objects, into distinct variables | |
> (({ a, c }) => ({ a, c }))({'a': 0, 'b': 1, 'c': 2}) | |
{a: 0, c: 2} | |
// CLONING | |
> A = {a: 1} | |
> B = Object.assign({}, a) | |
> A.a = 0 | |
> B.a | |
1 | |
// All arrays are objects, checking the constructor property is fast for JavaScript engines | |
> arr = [] | |
> arr.constructor === Array | |
true | |
> A = {arr: []} | |
> A.arr && A.arr.constructor === Array | |
true | |
> A = [1] | |
> B = A.slice() | |
> A[0] = 0 | |
> B | |
[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment