Published
Edited
Oct 19, 2019
Insert cell
Insert cell
Insert cell
Insert cell
primitivesArray = [1,2,3]
Insert cell
primitivesObject = ({x:1, y:2, z: 3})
Insert cell
objectInArray = [{x:1,y:2,z:3}]
Insert cell
nestedObject = ({ x: {z:1}, y: 2})
Insert cell
nestedObjectInArray = [{ x:{z:1} , y: 2}]
Insert cell
nestedObjectWithDates = ({a: {date: new Date()}})
Insert cell
Insert cell
Insert cell
primitivesCopy = Array.from(primitivesArray)
Insert cell
primitivesCopy[0] = 0
Insert cell
primitivesCopy
Insert cell
primitivesArray
Insert cell
Insert cell
testAssignPrimitivesDestination = Object.assign({}, primitivesObject)
Insert cell
testAssignPrimitivesDestination
Insert cell
testAssignPrimitivesDestination.x = 0

Insert cell
primitivesObject
Insert cell
Insert cell
objectInArrayCopyUsingFrom = Array.from(objectInArray)
Insert cell
objectInArrayCopyUsingFrom[0].x = 0
Insert cell
JSON.stringify(objectInArray)
Insert cell
JSON.stringify(objectInArrayCopyUsingFrom)
Insert cell
Insert cell
nestedObjectCopyUsingAssign = Object.assign({}, nestedObject)
Insert cell
nestedObjectCopyUsingAssign.x.z = 0
Insert cell
JSON.stringify(nestedObject)
Insert cell
JSON.stringify(nestedObjectCopyUsingAssign)
Insert cell
Insert cell

md`### \`JSON.parse(JSON.stringify)\` deep copies nested objects from source
`
Insert cell
JSON.stringify(nestedObject)
Insert cell
nestedObjectCopyUsingJSONParse = JSON.parse(JSON.stringify(nestedObject))
Insert cell
nestedObjectCopyUsingJSONParse.x.z = 100
Insert cell
JSON.stringify(nestedObject)
Insert cell
JSON.stringify(nestedObjectCopyUsingJSONParse)
Insert cell
md`### \`JSON.parse(JSON.stringify)\` deep copies array containing object with nested objects
`
Insert cell
nestedObjectInArrayCopyUsingJSONParse = JSON.parse(JSON.stringify(nestedObjectInArray))
Insert cell
nestedObjectInArrayCopyUsingJSONParse[0].x.z = 0
Insert cell
JSON.stringify(nestedObjectInArray)
Insert cell
JSON.stringify(nestedObjectInArrayCopyUsingJSONParse)
Insert cell
md`### Nested \`Date\` objects are stringified in a \`JSON.parse(JSON.stringify)\` deep copy`
Insert cell
nestedObjectWithDatesCopy = deepCopyObjectViaParseStringify(nestedObjectWithDates)
Insert cell
JSON.stringify(nestedObjectWithDatesCopy)
Insert cell
md`### \`Object.create()\` sets source object as prototype of destination object, therefore not affecting the source during modifications`
Insert cell
testObject = ({x:1,y:2,z:3})
Insert cell
testCreate = Object.create(testObject)
Insert cell
//testCreate.x = 0
Insert cell
testCreate.y
Insert cell
testCreate
Insert cell
JSON.stringify(testCreate, Object.keys(testCreate.__proto__))
Insert cell
md`### Utility functions for deep copy of object
- Nested object references are serialized and assigned to a new object
- Deep copy does not refer to existing objects i.e. no sharing of data with original object
- TLDR: Nested objects are duplicated
`
Insert cell
function deepCopyObject(obj) {
let copy = {}
for(let i in obj) {
if(obj[i] && typeof(obj[i] === "object")) {
copy[i] = deepCopyObject(obj[i])
} else {
copy[i] = obj[i]
}
}
return copy
}

Insert cell
deepCopyObjectViaParseStringify = (obj) => {
return JSON.parse(JSON.stringify(obj))
}
Insert cell
Insert cell
Insert cell
plantheidea = import('fast-equals')
Insert cell
plantheidea.deepEqual({ foo: 'bar' }, { foo: 'bar' })
Insert cell
function Circular(value) {
this.present = {
value,
deeply: {
nested: {
reference: this
}
}
}
}
Insert cell
plantheidea.circularDeepEqual(new Circular('foo'), new Circular('foo'))
Insert cell
plantheidea.circularDeepEqual(new Circular('foo'), new Circular('bar'))
Insert cell
testCircularShallow = ['foo']
Insert cell
testCircularShallow.push(testCircularShallow)
Insert cell
plantheidea.circularShallowEqual(testCircularShallow, ['foo', testCircularShallow])
Insert cell
plantheidea.circularShallowEqual(testCircularShallow, [testCircularShallow])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more