Skip to content

Instantly share code, notes, and snippets.

View DoctorDerek's full-sized avatar
☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com

Dr. Derek Austin DoctorDerek

☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / Set-array-one-liner.js
Created October 30, 2020 22:20
Find unique items by making a Set from a JavaScript array as a one-liner
Array.from(new Set([37, 37, 3700, 3700])) // [37,3700]
@DoctorDerek
DoctorDerek / use-Set-for-unique-object-references-in-an-array.js
Created October 30, 2020 23:54
JavaScript considers different objects with the same value to be unique items with different object references when using Set.
// Different JavaScript objects have different
// object references, even with the same values:
const someArray = [1]
const otherArray = [1]
// Different references even though both are [1]
console.log(`Does someArray === someArray?`)
console.log(`${someArray === someArray}`)
// Output: true because of same object reference
console.log(`Does someArray === otherArray?`)
@DoctorDerek
DoctorDerek / use-Set-array-spread-operator-one-liner.js
Created November 2, 2020 19:26
Use Set to make an Array with the ... spread operator as a one-liner
[...new Set([3, 4, 5, 5])] // [3,4,5]
@DoctorDerek
DoctorDerek / methods-to-iterate-through-a-set.js
Created November 2, 2020 23:22
Three methods to iterate through a JavaScript ES6 Set object
const emojiSet = new Set(["🙋🏼", "🦾", "🦿", "🙋🏼"])
//  Use Set.prototype.keys() or Set.prototype.values()
const emojiIterator = emojiSet.values()
// You can use a for...of loop with an Iterator object
for (const emoji of emojiIterator) {
console.log(emoji)
}
// emojiSet.keys() is equivalent to: emojiSet.values()
for (const emoji of emojiSet.keys()) {
@DoctorDerek
DoctorDerek / Using Set with an Array of Objects to Find Unique Values.js
Created November 3, 2020 19:49
How to Use Set with an Array of Objects to Find The Unique Values Among Those Objects
const yearsArray = [{ year: 1999 }, { year: 2000 }, { year: 1999 }]
const arrayOfOnlyYears = yearsArray.map((object) => object.year)
const uniqueYears = [...new Set(arrayOfOnlyYears)]
console.log(uniqueYears) // [1999,2000]
@DoctorDerek
DoctorDerek / use-Set-with-an-array-for-unique-primitives.js
Last active November 4, 2020 19:29
Using Set for unique primitive values in an array in JavaScript
// Set up a number array with duplicate values
const myNumberArray = [37, 37, 370, 3700, 3700, 37]
// Using Set with different approaches
// 1) Set using for...of loop
const myNumberSetForOf = new Set()
for (const myNumber of myNumberArray) {
myNumberSetForOf.add(myNumber)
}
// 2) Set using .forEach()
@DoctorDerek
DoctorDerek / finding-unique-object-keys-or-values-with-set.js
Last active November 4, 2020 19:46
How to find unique object values using Set in JavaScript ES6
// You can use Object.values() with Set to find unique
// values in an object, but keys are always unique.
const myObject = { hello: "🌏", hi: "🌏", howdy: "🚯" }
console.log(new Set(Object.keys(myObject)))
// Output: Set(3) [ "hello", "hi", "howdy" ]
console.log(new Set(Object.values(myObject)))
// Output: Set(2) [ "🌏", "🚯" ]
console.log(new Set(Object.entries(myObject)))
// Output: Set(3) [[ "hello", "🌏" ], ...]
@DoctorDerek
DoctorDerek / .gitignore
Created November 8, 2020 19:06
.gitignore file
# Markdown files managed by DatoCMS
src/pages/
# Secret API keys for Auth0 testing locally
.env.development
@DoctorDerek
DoctorDerek / gatsby-config.js
Created November 8, 2020 19:17
Modified Gatsby config file part 2
},
{
resolve: `gatsby-theme-auth0`,
options: {
domain: process.env.AUTH0_DOMAIN,
clientID: process.env.AUTH0_CLIENT_ID,
redirectUri: process.env.AUTH0_CALLBACK_URL,
// Optional fields:
// audience: process.env.AUTH0_AUDIENCE,
// responseType: process.env.AUTH0_RESPONSE_TYPE,
@DoctorDerek
DoctorDerek / gatsy-config.js
Created November 8, 2020 19:22
Modified Gatsby config file part 2
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})