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 / window.location.replace.html
Created July 17, 2022 15:51
How To Redirect to Another Webpage Using JavaScript To Change the URL https://medium.com/p/2b92754845a3
<!DOCTYPE html>
<html>
<head>
<title>Redirect example</title>
<meta charset="UTF-8" />
</head>
<body onLoad={window.location.replace('https://google.com')}>
Redirecting...
@DoctorDerek
DoctorDerek / window.location.href.html
Last active July 17, 2022 15:51
How To Redirect to Another Webpage Using JavaScript To Change the URL https://medium.com/p/2b92754845a3
<button
onClick={() => {
window.location.href = 'https://google.com'
}}
>
Google
</button>
const customDeepCopy = (inObject) => {
let outObject, value, key;
if (typeof inObject !== "object" || inObject === null) {
return inObject; // Return the value if inObject is not an object
}
// Create an array or object to hold the values
outObject = Array.isArray(inObject) ? [] : {};
for (key in inObject) {
value = inObject[key];
// Recursively (deep) copy for nested objects, including arrays
// Try this code sample at https://npm.runkit.com/ramda
const ramda = require("ramda")
// Start with a deeply nested array object:
const array = [37, {a: "b"}, {b: {c: "d"}}]
// Make a deep copy with Ramda:
const copy = ramda.clone(array)
console.info(array) // [37, {a: "b"}, {b: {c: "d"}}]
copy[0] = -0 // Change a primitive value (not nested)
copy[1].a = "y" // Change a deeply nested value
copy[2].b.c = "z" // Change another deeply nested value
// Try this code sample at https://npm.runkit.com/lodash.clonedeep
const lodashClonedeep = require("lodash.clonedeep")
// Start with a deeply nested array object:
const array = [37, {a: "b"}, {b: {c: "d"}}]
// Make a deep copy with lodashClonedeep:
const copy = lodashClonedeep(array)
console.info(array) // [37, {a: "b"}, {b: {c: "d"}}]
copy[0] = -0 // Change a primitive value (not nested)
copy[1].a = "y" // Change a deeply nested value
copy[2].b.c = "z" // Change another deeply nested value
// Start with a deeply nested array object:
const array = [37, {a: "b"}, {b: {c: "d"}}]
// Make a shallow copy with Array.from():
const copy = Array.from(array)
console.table(array) // [37, {a: "b"}, {b: {c: "d"}}]
copy[0] = -0 // Change a primitive value (not nested)
copy[1].a = "y" // Change a deeply nested value
copy[2].b.c = "z" // Change another deeply nested value
// Deeply nested objects weren't actually copied:
console.table(array) // [37, {a: "y"}, {b: {c: "z"}}]
const safeToCopy = ["1", 2, [true]]
const goodCopy = JSON.parse(JSON.stringify(safeToCopy))
console.info(safeToCopy) // ["1", 2, [true]]
console.info(goodCopy) // ["1", 2, [true]]
// Since we're working with JSON-safe data, the deep copy is valid:
const copyDeux = JSON.parse(JSON.stringify(safeToCopy))
copyDeux[2][0] = false // Change deeply-nested [true] to [false]
console.info(safeToCopy) // ["1", 2, [true]]
const notSafeToCopy = [new Date(), undefined, [Infinity]]
const copy = JSON.parse(JSON.stringify(notSafeToCopy))
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]]
console.info(copy) // ["2022-04-24T20:09:16.309Z", null, [null]]
// At least we did make a deep copy, but the data is all wrong:
const copyDos = JSON.parse(JSON.stringify(notSafeToCopy))
copyDos[2][0] = -Infinity // Change deeply-nested [null] to [-Infinity]
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]]
@DoctorDerek
DoctorDerek / const lodashClonedeep = require("lodash.clonedeep").js
Created April 24, 2022 01:30
// eslint-disable-next-line @typescript-eslint/no-var-requires const lodashClonedeep = require("lodash.clonedeep")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const lodashClonedeep = require("lodash.clonedeep")
@DoctorDerek
DoctorDerek / import lodashClonedeep from "lodash.clonedeep".js
Created April 24, 2022 01:29
import lodashClonedeep from "lodash.clonedeep".js
import lodashClonedeep from "lodash.clonedeep"