Published
Edited
Oct 19, 2019
Insert cell
Insert cell
md`## Sorting Arrays
\`Array.sort()\` is a deadly sort. Also it sors elements alphabetically, so to sort numbers, pass a callback function
`
Insert cell
numbers = [11,2,3,4,5,6,7,8]
Insert cell
numbers.sort()
Insert cell
numbers.sort((a1, a2) => a1 - a2)
Insert cell
Insert cell
testreduce = [10, 2, -30, 4, 55, 6, 7, 8]
Insert cell
testreduce.reduce((x, y) => x > y ? x : y)
Insert cell
md`### Sum array elements`
Insert cell
testreduce.reduce((x, y) => x + y)
Insert cell
md`### Find number in array greater than a given number`
Insert cell
testreduce2 = [10, 2, -30, 4, 550, 6, 7, 8]
Insert cell
testreduce2.reduce((x, y) => x > y ? x : y, 100)
Insert cell
Insert cell
objectInArray = [{x: 1,y: 2,z: 3}]
Insert cell
primitiveArray = [1,2,3,5,6]
Insert cell
Insert cell
destinationFromDestructuring = [...primitiveArray, 700]
Insert cell
destinationFromDestructuring[0] = 100
Insert cell
primitiveArray
Insert cell
Insert cell
objectInArrayDestructuring = [...objectInArray]
Insert cell
objectInArrayDestructuring[0].x = 5
Insert cell

JSON.stringify(objectInArrayDestructuring)
Insert cell
JSON.stringify(objectInArray)
Insert cell
md`### Primitives array deep copy using \`Array.slice()\``
Insert cell
sourceForSlicing = [...primitiveArray]
Insert cell
destinationFromSlice = sourceForSlicing.slice()
Insert cell
destinationFromSlice[0] = 100
Insert cell
sourceForSlicing
Insert cell
md`### Object in array not deep copied using Array.slice()`
Insert cell
objectInArraysSliceCopy = objectInArray.slice()
Insert cell
objectInArraysSliceCopy[0].x = 5
Insert cell
JSON.stringify(objectInArray)
Insert cell
JSON.stringify(objectInArraysSliceCopy)
Insert cell
Insert cell
sourceObj = ({a: {b: 5}})
Insert cell
sourceObjWithArray = ({a: {b: [1,2,3]}})
Insert cell
destinationObj = ({...sourceObj})
Insert cell
destinationObj.a.b = 50
Insert cell
JSON.stringify(sourceObj)
Insert cell
JSON.stringify(destinationObj)
Insert cell
destinationObjWithArray = ({...sourceObjWithArray})
Insert cell
destinationObjWithArray.a.b.push(4)
Insert cell
JSON.stringify(sourceObjWithArray)
Insert cell
JSON.stringify(destinationObjWithArray)
Insert cell
md`## \`in\` operator
### Check for object property
`
Insert cell
car = ({make: 'Honda', model: 'Accord', year: 1998})
Insert cell
'make' in car
Insert cell

md`### Check for index in array`
Insert cell
lengthFour = [1,2,3,4]
Insert cell
`${4}` in lengthFour
Insert cell
`${3}` in lengthFour
Insert cell
md`## \`Set\` data structure
### Find unique letters in a string
`
Insert cell
temp = new Set('abcaacb')
Insert cell
[...temp]
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