Skip to content

Instantly share code, notes, and snippets.

@okovalov
Created December 18, 2019 19:37
Show Gist options
  • Save okovalov/a777c0e3b884e2da151ea0f314664c72 to your computer and use it in GitHub Desktop.
Save okovalov/a777c0e3b884e2da151ea0f314664c72 to your computer and use it in GitHub Desktop.
// 1. quick convert Number to String
const age = 18 + ""
console.log(typeof age) // string
// 3. fill arrays
const users = Array(5).fill('')
console.log(users); // [ '', '', '', '', '' ]
// 3. unique values out of an array
const values = ['Ed', 'daddy', 'Anna', 'tech lead', 'Anna', 'John Dough']
const unique = Array.from(new Set(values))
console.log(unique); // [ 'Ed', 'daddy', 'Anna', 'tech lead', 'John Dough' ]
// 4. dynamic objects properties
const dynamic = 'hobbies'
const user = {
name: 'Ed',
email: '[email protected]',
[dynamic]: 'reading'
}
console.log(user); // { name: 'Ed', email: '[email protected]', hobbies: 'reading' }
// 5. slicing an array
const folks = [1, 2, 3, 4, 5]
folks.length = 3
console.log(folks); // [ 1, 2, 3 ]
// 6. slicing arrays from the end
const folksTwo = [1, 2, 3, 4, 5]
console.log(folksTwo.slice(-2)); // [ 4, 5 ]
// 7. Array to object
const folksThree = [1, 2, 3, 4, 5]
const folksObject = { ...folksThree }
console.log(folksObject); // { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
// 8. Object ot array
const userObjectAgain = {
name: 'Ed',
email: '[email protected]',
hobbie: 'reading'
}
const userAgain = Object.values(userObjectAgain)
console.log(userAgain); // [ 'Ed', '[email protected]', 'reading' ]
// 9. performance (execution time)
let startAt = performance.now()
for (let i = 0; i < 200000; i++) {
console.log(i)
}
let endAt = performance.now()
console.log(`${endAt - startAt} took milliseconds to execute`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment