Last active
October 27, 2017 20:10
-
-
Save arturparkhisenko/c79f12f0c4fcc855671ed65cdcd86bdf to your computer and use it in GitHub Desktop.
js-hints-object
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
// https://www.youtube.com/watch?v=5Kw4XVSb4P4 | |
// Cloning objects, not deep! use clone for that https://gist.github.com/arturparkhisenko/db604e314ea8ff8ba065 | |
const clonedR = {...obj}; | |
const cloneOS = Object.assign({}, obj); | |
// Merging objects (also filling with defaults) | |
const mergedR = {...obj1, ...obj2}; | |
const mergedOS = Object.assign({}, obj1, obj2); | |
// Non-destructively updating property `foo` | |
// with spread | |
const obj1 = {foo: 'a', bar: 'b'}; | |
const obj2 = {...obj1, foo: 1}; // {foo: 1, bar: 'b'} | |
// or with Object.Assign | |
const obj2 = Object.assign({}, obj1, {foo: 1}); // {foo: 1, bar: 'b'} | |
// Safely Accessing Deeply Nested Values In JavaScript | |
// https://medium.com/javascript-inside/safely-accessing-deeply-nested-values-in-javascript-99bf72a0855a | |
const props = { | |
user: { | |
posts: [ | |
{ title: 'Foo', comments: [ 'Good one!', 'Interesting...' ] }, | |
{ title: 'Bar', comments: [ 'Ok' ] }, | |
{ title: 'Baz', comments: [] } | |
] | |
} | |
}; | |
// function: | |
const get = (p, o) => | |
p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o); | |
// let's pass in our props object... | |
console.log(get(['user', 'posts', 0, 'comments'], props)); | |
// [ 'Good one!', 'Interesting...' ] | |
console.log(get(['user', 'post', 0, 'comments'], props)); | |
// null | |
// DESTRUCTURING -------------------------------------------------------------- | |
const cat = {sound: 'meow'}; | |
const speak = ({sound}) => console.log(sound); | |
speak(cat); // => 'meow' | |
{ | |
// in general we have the same argument variable name as the object | |
// property name, | |
// http://exploringjs.com/es6/ch_destructuring.html#sec_overview-destructuring | |
// {prop} is short for {prop: prop} | |
// example short: | |
let {sound} = cat; | |
console.warn(sound); | |
// example full: | |
let sound; | |
({sound} = cat); // that's why i thought it'll destruct it at the argument stage, and make something like: | |
const speak = sound => console.log(sound); | |
} | |
// concat -------------------------------------------------------------- | |
// https://twitter.com/YDKJS/status/922203387000709120/photo/1 | |
// unfortunate syntax gotcha: can't use `...` spread in a ternary. a case where `[].concat(..)` is preferable. | |
let a = [1, 2, 3]; | |
let b = [4, 5]; | |
let c = [...a, Array.isArray(b) ? ...b : b]; // SyntaxError: Unexpected token ... | |
let d = [...a, ...(Array.isArray(b) ? ...b : b)]; // or | |
let e = a.concat(b); | |
// https://twitter.com/anatudor/status/923973815314583555 | |
a.filter((c, i) => a.indexOf(c) == i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment