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 / Using typeof to catch undeclared variables when checking for an Array.js
Created November 17, 2020 19:54
Using typeof to catch undeclared variables when checking for an Array
// Declared array example using typeof:
const declaredArray = []
let isThatAnArray = false
console.log(typeof declaredArray) // object
if (typeof declaredArray === "object") {
// Alternatively, typeof declaredArray !== "undefined"
// would screen out undefined & undeclared variables
isThatAnArray = Array.isArray(declaredArray)
}
console.log(isThatAnArray) // true
@DoctorDerek
DoctorDerek / Checking for an empty array with vanilla JavaScript and Array.isArray.js
Created November 17, 2020 19:55
Checking for an empty array with vanilla JavaScript and Array.isArray
const emptyArray = []
const fullArray = [777]
const notAnArray = 777
if (Array.isArray(emptyArray)) {
if (emptyArray.length > 0) {
console.log("Non-empty array")
}
}
@DoctorDerek
DoctorDerek / Using Set for Object Properties.js
Created November 17, 2020 23:41
Using Set for Object Properties
// Make a new Set with new Set():
const myUniqueColors = new Set()
const myObject1 = { color: "🔴" }
const myObject2 = { color: "🔵" }
const myObject3 = { color: "🟢" }
const myObject4 = { color: "🔵" }
const myObject5 = { color: "🟢" }
// Add a value to the Set with .add():
myUniqueColors.add(myObject1.color)
@DoctorDerek
DoctorDerek / Find Unique Values for a Given Property for Each Object in an Array of Objects.js
Created November 18, 2020 00:29
Find Unique Values for a Given Property for Each Object in an Array of Objects
const myObjects = [
{ color: "💛" },
{ color: "💛" },
{ color: "💜" },
{ color: "🧡" },
{ color: "💜" },
{ color: "🧡" },
]
// We could use a for...of loop:
@DoctorDerek
DoctorDerek / One-Liner Find Unique Values for a Given Property for Each Object in an Array of Objects.js
Created November 18, 2020 00:31
One-Liner Find Unique Values for a Given Property for Each Object in an Array of Objects.
// One-liner to find unique object properties with Set:
console.log([...new Set(myObjects.map((o) => o.color))])
// Output: Array(3) [ "💛", "💜", "🧡" ]
@DoctorDerek
DoctorDerek / ParentComponent.jsx
Last active November 6, 2023 16:54
ParentComponent.jsx - How to Pass All Props to a Child Component in React
import React from "react"
import DisplayAllProps from "./DisplayAllProps"
import ChildComponent from "./ChildComponent"
const ParentComponent = (props) => (
<section>
<h1>ParentComponent's props:</h1>
<DisplayAllProps {...props}></DisplayAllProps>
<ChildComponent {...props}></ChildComponent>
@DoctorDerek
DoctorDerek / App.jsx
Last active November 27, 2020 20:21
App.jsx - How to Pass All Props to a Child Component in React
import React from "react"
import "./styles.css"
import ParentComponent from "./ParentComponent"
const App = () => (
<div className="App">
<ParentComponent name="Johnny" job="👨‍🎤">
🌟
</ParentComponent>
</div>
@DoctorDerek
DoctorDerek / ChildComponent.jsx
Last active November 27, 2020 20:21
ChildComponent.jsx - How to Pass All Props to a Child Component in React
import React from "react"
import DisplayAllProps from "./DisplayAllProps"
const ChildComponent = (props) => (
<React.Fragment>
<h1>ChildComponent's props:</h1>
<DisplayAllProps {...props}></DisplayAllProps>
</React.Fragment>
)
@DoctorDerek
DoctorDerek / DisplayAllProps.jsx
Last active November 27, 2020 20:21
DisplayAllProps.jsx - How to Pass All Props to a Child Component in React
import React from "react"
const DisplayAllProps = (props) => (
<table>
<tbody>
{Array.from(Object.entries(props)).map(([key, value]) => (
<tr key={key + Math.random()}>
<td>{key}</td>
<td>{value}</td>
</tr>
@DoctorDerek
DoctorDerek / ParentComponentNoSpread.jsx
Last active November 27, 2020 20:37
ParentComponentNoSpread.jsx - How to Pass All Props to a Child Component in React
import React from "react"
import DisplayAllProps from "./DisplayAllProps"
import ChildComponent from "./ChildComponent"
const ParentComponent = (props) => (
<section>
<h1>ParentComponent's props:</h1>
<DisplayAllProps
name={props.name}