Skip to content

Instantly share code, notes, and snippets.

View prof3ssorSt3v3's full-sized avatar
🎯
Focusing

Steve Griffith prof3ssorSt3v3

🎯
Focusing
View GitHub Profile
//Array some()
// Wider availability than includes()
// functional approach like map, filter, and forEach
// returns Boolean when first positive match is found
let movies = ['Star Wars', 'Star Trek', 'Jaws', 'Jurassic Park', 'Gross Pointe Blank', 'Eternal Sunshine of the Spotless Mind', 'Memento', 'Dog Soldiers', 'The Host', 'Gran Torino', 'Close Encounters of the Third Kind', 'Good Will Hunting', 'Layer Cake', 'Casino Royale', 'Almost Famous'];
let keyword1 = 'Star'; //appears more than once
let keyword2 = 'Cake'; //appears once
let keyword3 = 'Max'; //not in list
//Array every()
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
//checks EVERY element to see if it returns true
let numbers = [40, 16, 67, 345, 22, 23, 142, 63, 59, 283];
let greaterThan20 = numbers.every(function(num){
return num > 20;
});
console.log(greaterThan20);
//Array reduce method
//reduce all the values in an array into a single result
// Uses a callback function just like map, forEach, filter, etc
// array.reduce(callback, initialValue)
// also has a second parameter which is an initialValue
let numbers = [12, 34, 56, 78, 91];
//find the sum of all the numbers
// ES6 (fat) Arrow functions
//work best in callback function scenarios
// parentheses around input if more than one
// curly braces around function body if more than one line/command
// return only needed if more than one line of code
let numbers = [123, 234, 345, 456, 567];
let names = ['Alex', 'Bree', 'Cara', 'Cole', 'Devon', 'Riley'];
//simple function syntax
//Arrays of Objects
// Efficiently combining and chaining Array methods and Arrow functions
// person.email.indexOf("@replicant.io") > -1
//Find the names of all the people who have "replicant.io" emails
let people = [
{"id":123, "name":"Rick Deckard", "email":"rick@bladerunner.org"},
{"id":456, "name":"Roy Batty", "email":"roy@replicant.io"},
{"id":789, "name":"J.F. Sebastian", "email":"j.f@tyler.com"},
{"id":258, "name":"Pris", "email":"pris@replicant.io"}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document Fragments</title>
<style type="text/css">
html{
font-size: 120%;
font-family: BlinkMacSystemFont, -apple-system, “Segoe UI”, Roboto, Helvetica, Arial, sans-serif;
//SPREAD and REST operators
//Added with ES6
// ...
// two sides of the same operation
let beers = ['Corona', 'Heineken'];
let stuff = [22, 'Bob', true, beers];
//SPREAD - expands an array into its elements
myFunc( ...stuff );
//basic fetch
//using jsonplaceholder for the data
//Remember that fetch returns a promise
//HTTP status codes - http://www.restapitutorial.com/httpstatuscodes.html
//to test with NODE we need to install the node-fetch package
// npm install node-fetch
//let fetch = require('node-fetch');
//get the details for a random user
var uri = 'http://jsonplaceholder.typicode.com/users/7';
//none of these examples do proper error handling of
//nasty http status codes or
//invalid data types - text, xml, json, etc
//*********************************************************
//OLD Version AJAX
var xhr = new XMLHttpRequest();
//Array concat method
let a = [2, 4, 6, 8];
let b = [1, 3, 5];
//for()
//for( in )
//a.forEach() push() unshift()
let c = a.concat(b);