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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Redirect example</title> | |
<meta charset="UTF-8" /> | |
</head> | |
<body onLoad={window.location.replace('https://google.com')}> | |
Redirecting... |
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
<button | |
onClick={() => { | |
window.location.href = 'https://google.com' | |
}} | |
> | |
</button> |
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 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 |
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
// 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 |
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
// 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 |
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
// 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"}}] |
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 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]] |
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 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]] |
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
// eslint-disable-next-line @typescript-eslint/no-var-requires | |
const lodashClonedeep = require("lodash.clonedeep") |
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
import lodashClonedeep from "lodash.clonedeep" |