This file contains hidden or 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
// 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() |
This file contains hidden or 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
Array.from(new Set([37, 37, 3700, 3700])) // [37,3700] |
This file contains hidden or 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
// 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?`) |
This file contains hidden or 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
[...new Set([3, 4, 5, 5])] // [3,4,5] |
This file contains hidden or 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
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()) { |
This file contains hidden or 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
// 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", "🌏" ], ...] |
This file contains hidden or 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
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] |
This file contains hidden or 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
// 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`) |
This file contains hidden or 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
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: "🎷" } ] |
This file contains hidden or 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
// 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(): |
OlderNewer