This file contains 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
// with .concat you can Add values(of mixed types) to an Array | |
var items = [1,2]; | |
var newItems = items.concat(3,4,'string', undefined); | |
console.log(newItems); // [1, 2, 3, 4, 'string', undefined] | |
// .concat can also take in other arrays | |
var newArrayItems = items.concat([3,4], [5,6,7], 8,9); | |
console.log(newArrayItems); // [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
// Practical USE CASE: |
This file contains 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
// Array.prototype.join(); | |
var name = ['Shane', 'Osbourne']; | |
// give all the values back as a string and I provide the separator in between | |
console.log(names.join(' ')); // Shane Osbourne with a space in between | |
console.log(names.join('-')); // Shane-Osbourne | |
console.log(names.join('')); // ShaneOsbourne | |
console.log(names.join()); // when not providing any arg at all, you get the values separated with a comma -> Shane,Osbourne |
This file contains 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
// BAD - the dogYear variable leaks out to the window scope | |
var age = 100; | |
if(age > 12) { | |
var dogYears = age * 7; | |
console.log(`You are ${dogYears} dog years old!`); | |
} | |
console.log(dogYears); // 700 | |
// GOOD - the dogYears variable stays available only to the block | |
var age = 100; |
This file contains 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
// This is used when you want to make your program private (nothing leaks into the global scope of the window) | |
// BEFORE - using an IFY | |
(function(){ | |
var name = 'mara'; | |
console.log(name); | |
})(); | |
// BETTER - using a block | |
{ |
This file contains 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
// BAD | |
for(var i = 0; i < 10; i++) { | |
console.log(i); // 0 1 2 3 4 5 6 7 8 9 | |
setTimeout(function() { | |
console.log('The number is ' + i); | |
},1000); | |
} // -> 'The number is 10' printed 10 times | |
// GOOD | |
for(let i = 0; i < 10; i++) { |
This file contains 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
// 1. Transform a nested list into a single list = Flattten a list of lists | |
var data = [[1, 2, 3], [4, 5, 6], [7, 8]]; | |
// flaten this array of arrays into a SINGLE array | |
var flattenedData = data.reduce(function(acc, value){ | |
return acc.concat(value); | |
},[]); |
This file contains 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
// Array.prototype.some() | |
var tasks = [ | |
{ | |
title: 'Do laundry', | |
completed: true | |
}, | |
{ | |
title: 'Feed the cat', | |
completed: true |
This file contains 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 React from 'react' | |
import axios from 'axios' | |
const endpoint = 'https://raw.githubusercontent.com/ongithub/products/gh-pages/products.json' | |
class ProductList extends React.Component { | |
state = { | |
allProducts: [] | |
} | |
// when component is mounted => fetchData |
This file contains 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
Commands: | |
// initialize a new vue project | |
vue init webpack chart | |
// install dependencies | |
cd chart && yarn install | |
// install the chart libraries | |
yarn add vue-chartjs chart.js |
This file contains 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
// ** This project demonstrates how to loop through a simple data set and display a table ** | |
Commands for setup: | |
// initialize a simple new vue project (simple webpack + vue-loader setup for quick prototyping) | |
vue init webpack-simple my_table | |
// install dependencies & run | |
cd my_table && yarn install && yarn run dev |
OlderNewer