Skip to content

Instantly share code, notes, and snippets.

View MaraAlexa's full-sized avatar

Mara Alexa MaraAlexa

View GitHub Profile
@MaraAlexa
MaraAlexa / concat.js
Last active April 29, 2017 09:21
Use .concat to Add Values to an Array
// 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:
@MaraAlexa
MaraAlexa / join.js
Created April 29, 2017 09:20
Combine Values of an Array into a String with Join
// 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
@MaraAlexa
MaraAlexa / scoping.js
Created May 5, 2017 11:43
ES6 : var vs. let and const
// 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;
@MaraAlexa
MaraAlexa / scoping.js
Created May 5, 2017 11:57
ES6: replacing an IIFE (immediadly invoked function expression) with a block
// 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
{
@MaraAlexa
MaraAlexa / scoping.js
Created May 5, 2017 12:06
ES6: for loop issue solved
// 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++) {
@MaraAlexa
MaraAlexa / reduce_nested_list_to_single_array.js
Last active May 12, 2017 13:20
Reduce Data with Array.reduce()
// 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);
},[]);
// Array.prototype.some()
var tasks = [
{
title: 'Do laundry',
completed: true
},
{
title: 'Feed the cat',
completed: true
@MaraAlexa
MaraAlexa / ProductList.js
Last active October 4, 2017 16:40
consume an endpoint with axios and put the data into state
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
@MaraAlexa
MaraAlexa / chart.js
Last active October 10, 2017 16:19
Vue Chart
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
@MaraAlexa
MaraAlexa / vue_table.js
Last active June 3, 2022 22:20
loop through data and display a table with vue.js
// ** 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