Published
Edited
Aug 25, 2021
1 fork
3 stars
Insert cell
Insert cell
Insert cell
function hello_standard(name) {
return "Hello "+name;
}
Insert cell
hello_standard("World")
Insert cell
hello_arrow = (name) => "Hello " + name
Insert cell
hello_arrow("World")
Insert cell
Insert cell
point = ({x:2, y:3})
Insert cell
Insert cell
point.x
Insert cell
point.y
Insert cell
Insert cell
point["x"]
Insert cell
point["y"]
Insert cell
Insert cell
getComponent = (p,e)=>p[e]
Insert cell
getComponent(point,"x")
Insert cell
getComponent(point,"y")
Insert cell
md`**Duplicate or copy Javascript Objects**:
\`Remember assignment only copies the pointer to the object. It does not duplicate the object content.\`

To copy an object in JavaScript, you have three options:
- Use the spread (...) syntax
- Use the Object.assign() method
- Use the JSON.stringify() and JSON.parse() methods
The following illustrates how to copy an object using these three methods:`
Insert cell
point
Insert cell
pointCopies = {
// using spread ...
let p1 = {
...point
};
// using Object.assign() method
let p2 = Object.assign({}, point);

let p3 = { p1, p2 };

// using JSON
let p4 = JSON.parse(JSON.stringify(p3));

p1.x = 4;
p2.x = 5;

return [p1, p2, p3, p4];
}
Insert cell
Insert cell
Object.keys(point)
Insert cell
Object.values(point)
Insert cell
Insert cell
Object.entries(point)
Insert cell
pointCopies.slice(0,2)
Insert cell
Insert cell
Insert cell
A = [2, 4, 6, 8, 10]
Insert cell
pointObjects = [
{x:2,y:3},
{x:4, y:1},
{x:-1, y: 2}
]
Insert cell
md`### Array Length
returns the lengh of array A : A.length`
Insert cell
A.length
Insert cell
md `### Array Slice: A.slice([start[,end]]))
Duplicate the array fully or partly from start to end.
Creates a new array from the elements of the original array. The original array remains unchanged.`
Insert cell
A.slice()
Insert cell
A.slice(4)
Insert cell
A.slice(2,4)
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
md`### Array Map: A.map(callbackFn)
Create a new array with results of calling function *callbackFn* on every element in A`
Insert cell
A
Insert cell
A.filter(d=> d>5)
Insert cell
A.map(e => 10 * e)
Insert cell
Insert cell
pointArray2 = pointObjects.map(d=>[d.x,d.y])
Insert cell
Xs = pointObjects.map(d=>d.x)
Insert cell
Insert cell
pointMagnitudes = pointObjects.map((d) => Math.sqrt(d.x * d.x + d.y * d.y))
Insert cell
md`### Array Filter: A.filter(callbackFn)
Create a new array with all those elements of array that pass the boolean operation defined in callbackFn.`
Insert cell
points1stQ = pointObjects.filter(d=>d.x>=0 && d.y>=0)
Insert cell
md`##### Note:
*callbackFn* gets the array element as the first parameter. Also gets the index of the element as the second parameter, if you wish to use. (It has a third parameter too. It is the original array that the array function was called upon!)`
Insert cell
Aeven = A.filter((d,i)=>(i%2==0))
Insert cell
md`## Array Reduce: A.reduce(reducerFn, initialValue)
This function executes a reducer function (that you provide) on each element of the array, returns a single output value.
The reducer functions takes one extra argument compared to other callback functions mentioned above. They are
- Accumulator (acc)
- Current Value (cur)
- Current Index (idx)
- Source Array (src)
The accumulator must be used in the reducerFn to accumulate value.
*initialValue* is the starting value that the Accumulator takes.
For example: to compute the average of a number array A, we can use the following statement:
(A.reduce((acc,cur)=>acc = acc+cur, 0))/A.length
`
Insert cell
A
Insert cell
A.slice(1,4)
Insert cell
sum = (A.slice(1).reduce((acc,cur)=>acc = acc+cur, 0))
Insert cell
covid = d3.csv(
"https://covid.ourworldindata.org/data/owid-covid-data.csv",
d3.autoType
)
Insert cell
covid.columns
Insert cell
[...new Set(covid.map(d=>d.continent))]
Insert cell
Object.keys(covid.reduce((o,d)=>{
o[d.continent] = d.continent;
return o;
},{}))
Insert cell
md `##### Thre are many more array functions. For more array functions refer to a cheet sheet line https://www.shortcutfoo.com/app/dojos/javascript-arrays/cheatsheet.`
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