Skip to content

Instantly share code, notes, and snippets.

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

Dr. Derek Austin DoctorDerek

☀️
Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / How to Convert a JavaScript Array to JSON Format.js
Created December 17, 2020 23:50
How to Convert a JavaScript Array to JSON Format
// To convert the entire array to JSON:
const myArray = ["💝", 5, true]
console.log(myArray)
// Output: Array(3) [ "💝", 5, true ]
const myJSONString = JSON.stringify(myArray)
console.log(myJSONString)
// Output: ["💝",5,true]
const parsedArray = JSON.parse(myJSONString)
// Using && will prevent nonsense code from being executed.
false && console.log(NoNsEnSe_CoDE) // nothing happens
// You'll frequently see && used for type-checking.
const bananas = "🍌🍌🍌🍌🍌"
typeof bananas === "string" && console.log(bananas) // 🍌🍌🍌🍌🍌
// For example, you might be expecting an array, not a string.
Array.isArray(bananas) && bananas.push("🍌") // nothing happens
@DoctorDerek
DoctorDerek / Can JavaScript Arrays Contain Different Types.js
Created December 17, 2020 23:16
Can JavaScript Arrays Contain Different Types
// With no type checks, the code seems to work fine.
const user1 = { id: 1, name: "Erowyn" }
const user2 = { id: 2, name: "Lavendula" }
const user3 = { id: 3, name: "Fralia" }
const userArray = [user1, user2, user3]
for (const user of userArray) {
console.log(`${user.id}: ${user.name}`)
}
// Output:
// 1: Erowyn
// The typeof null is "object" in JavaScript.
console.log(typeof null === "object") // true
// Let's look at type checking a Date object.
const date = new Date()
console.log(typeof date) // "object"
console.log(Object.prototype.toString.call(date)) // [object Date]
console.log(date.constructor) // function Date()
console.log(date.constructor.name) // "Date"
console.log(date instanceof Date) // true
// && can be used to prevent errors, because the error won't be thrown.
const nullBanana = null
try {
console.log(nullBanana.length)
} catch (e) {
console.log(e)
}
// Output: "TypeError: null has no properties."
// Using && short-circuits the code, so the TypeError doesn't occur.
const object = { "🔑key🔑": "💸value💸" }
console.log(typeof object) // "object"
console.log(object !== null) // true
console.log(object != null) // true
console.log(typeof null) // "object"
console.log(null !== null) // false
console.log(null != null) // false
console.log(typeof undefined) // "undefined"
@DoctorDerek
DoctorDerek / Are JavaScript Object Keys Ordered and Iterable?
Created December 14, 2020 23:06
Are JavaScript Object Keys Ordered and Iterable?
// Typical object declared with object literal notation
const myObject = { zap: "⚡" }
myObject.boom = "💣"
// ES5 version using for...in loop
for (const key in myObject) {
if (myObject.hasOwnProperty(key)) {
console.log(`myObject["${key}"] is ${myObject[key]}`)
}
}
// Output:
@DoctorDerek
DoctorDerek / Method 1 - Array.prototype.concat().js
Created December 9, 2020 20:57
Method 1 - Array.prototype.concat()
// Joining Two Strings with .concat()
const stringOne = "🎅🤶"
const stringTwo = "👩‍🚀👨‍🚀"
// Use .concat() to join the strings:
console.log(stringOne.concat(stringTwo))
// Output: 🎅🤶👩‍🚀👨‍🚀
// .concat() won't change the string:
console.log(stringOne) // 🎅🤶
@DoctorDerek
DoctorDerek / Method 2 - The ... Spread Operator.js
Created December 9, 2020 20:56
Method 2 - The ... Spread Operator
// Joining Two Arrays with ... Spread
const array1 = ["🎅", "🤶"]
const array2 = ["👩‍🚀", "👨‍🚀"]
// Use ... spread to join the arrays:
console.log([...array1, ...array2])
// Spread won't change either array:
console.log(array1)
console.log(array2)
@DoctorDerek
DoctorDerek / Find Unique Objects by Content (key-value pairs).js
Created December 7, 2020 19:08
Find Unique Objects by Content (key-value pairs)
// We can get Set to compare objects' key-value pairs using JSON
const duplicates = [{ hello: "🌞" }, { hello: "🌞" }]
const objectsJSON = duplicates.map((object) => JSON.stringify(object))
const objectsJSONSet = new Set(objectsJSON)
const uniqueJSONArray = Array.from(objectsJSONSet)
// Equivalent to: (using the spread operator)
// const uniqueJSONArray = [...objectsJSONSet]
const uniqueObjectsByContent = uniqueJSONArray.map((string) =>
JSON.parse(string)
)