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 / 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 / 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 / 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 / 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 / Using Set with an Array of Objects Unique Objects.js
Last active June 14, 2022 14:30
How to Use Set with an Array of Objects To Find Unique Objects with Different Keys and Values (Not Object References)
// This array contains 2 objects repeated twice each.
const objectsArray = [
{ id: 1, emoji: "🎸" },
{ id: 2, emoji: "🎷" },
{ id: 1, emoji: "🎸" },
{ id: 2, emoji: "🎷" },
]
// Each of these objects has a different object reference.
// That means each object is unique according to Set:
console.log(`${objectsArray.length} objects`)
@DoctorDerek
DoctorDerek / One-Liner to Use Set with an Array of Objects Unique Objects.js
Created November 4, 2020 19:57
Easy One-Liner to Use Set to Find Unique Objects within an Array of Objects by the Objects' Contents
const uniqueObjectsOneLiner = [
...new Set(objectsArray.map((o) => JSON.stringify(o))),
].map((string) => JSON.parse(string))
console.log(`${uniqueObjectsOneLiner.length} objects`)
// Output: 2 objects
console.log(...uniqueObjectsOneLiner)
// [ { id: 1, emoji: "🎸" }, { id: 2, emoji: "🎷" } ]
@DoctorDerek
DoctorDerek / Finding the Unique Dates in an Array Using Set.js
Last active May 28, 2021 01:51
How to Find the Unique Dates in an Array Using Set in JavaScript ES6 from https://medium.com/p/196c55ce924b by @DoctorDerek
// Create an array with three Date objects:
const datesArray = [
new Date(3005, 00, 01),
new Date(3001, 00, 01),
new Date(3001, 00, 01),
]
// Note that the second parameter to the Date()
// constructor is a monthIndex, so 0 is January.
// Output the dates by using .map() and .toDateString():