Golden nuggets of knowledge

Hot tips to upgrade your skills

Explore the art of writing elegant and maintainable code that sparks joy for developers and users alike. Master best practices, code refactoring, and design patterns to create software that stands the test of time.

Filter out repeated values

Use this code snippet to remove repeated values from an array.

unique.ts
  1. 1
  2. 2
  3. 3
const array = [1, 2, 3, 3, 3, 3, 4, 4, 5, 6, 7]
const uniqueArray = [...new Set(array)]
console.log(unique) // [1, 2, 3, 4, 5, 6, 7]

Clean falsy values out of arrays

Use the .filter() function to remove falsy values from an array.

filter.ts
  1. 1
  2. 2
  3. 3
const array = ['andre', false, 0, 'loves', null, undefined, 'you']
const cleanArray = array.filter(v => v)
console.log(cleanArray) // ['andre', 'loves', 'you']

Flatten an array of arrays

Use the .flat() function to flatten a deeply nested array of arrays.

flat.ts
  1. 1
  2. 2
  3. 3
const array = [1, [2, 3, [4, 5, [6, 7], 8], 9], 10]
const flattenedArray = array.flat(Infinity)
console.log(flattenedArray) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sort an array alphabetically

Use the .sort() and .localeCompare() functions to sort alphabetically.

sort.ts
  1. 1
  2. 2
  3. 3
const array = ['study', 'I', 'must']
const sortedArray = array.sort((a, b) => a.localeCompare(b))
console.log(sortedArray) // ['I', 'must', 'study']

Get exclusive coding tips that I only share with email subscribers

Golden nuggets of code knowledge you can read in 5 minutes. Delivered to your inbox every 2 weeks.

Guide to full-stack web development

Once you subscribe you'll get my free guide to modern full-stack web development and solve analysis paralysis from choosing which tools to use.

 

Got it, thanks!

“I thought the website was good. But the newsletter? Even better!”

Keeran Flanegan