Skip to content

Instantly share code, notes, and snippets.

View kevinchisholm's full-sized avatar

Kevin Chisholm kevinchisholm

View GitHub Profile
const cars = [
{ name: 'Mustang', maker: 'Ford', isSuv: false },
{ name: 'EcoSport', maker: 'Ford', isSuv: true },
{ name: 'CR-V', maker: 'Honda', isSuv: true }
];
const match = _.filter(cars, car => { return car.isSuv; });
console.log(match);
// 0: {name: "EcoSport", maker: "Ford", isSuv: true}
@kevinchisholm
kevinchisholm / lodash-sumBy-example-1.js
Last active December 20, 2019 11:23
Lodash.sumBy example 1
const cars = [
{ name: 'Mustang', cost: 50000, isSuv: false},
{ name: 'EcoSport', cost: 40000, isSuv: true},
{ name: 'CR-V', cost: 35000, isSuv: true}
];
const totalCosts = _.sumBy(cars, car => {
return car.cost;
});
@kevinchisholm
kevinchisholm / lodash-sumBy-example-2.js
Created December 20, 2019 11:24
Lodash.sumBy example 2
const cars = [
{ name: 'Mustang', cost: 50000, isSuv: false},
{ name: 'EcoSport', cost: 40000, isSuv: true},
{ name: 'CR-V', cost: 35000, isSuv: true}
];
const totalCosts = _.sumBy(cars, car => {
return car.isSuv ? car.cost : 0;
});
@kevinchisholm
kevinchisholm / lodash-sumBy-example-3.js
Created December 20, 2019 11:26
Lodash.sumBy example 3
const cars = [
{ name: 'Mustang', cost: 50000, isSuv: false},
{ name: 'EcoSport', cost: 40000, isSuv: true},
{ name: 'CR-V', cost: 35000, isSuv: true}
];
const totalCosts = _.sumBy(cars, car => {
return car.cost > 39000 ? car.cost : 0;
});
const cars = [
{ name: 'Mustang', cost: 50000, isSuv: false},
{ name: 'EcoSport', cost: 40000, isSuv: true},
{ name: 'CR-V', cost: 35000, isSuv: true}
];
const totalCosts = _.sumBy(cars, car => {
return car.cost > 35000 ? car.cost : 0;
});
@kevinchisholm
kevinchisholm / load-moment.js
Created December 20, 2019 11:47
Loading moment.js in the console
function getMoment(t){var e=document.createElement("script");e.onload=t,e.src="http://bit.ly/moment-2-24-0-min",document.body.appendChild(e)}
getMoment(() => {
// put your code here...
});
@kevinchisholm
kevinchisholm / addEventListener.example-1.js
Created December 20, 2019 12:51
addEventListener Example 1
const body = document.getElementsByTagName('body')[0];
body.addEventListener('click', () => {
console.log('The <body /> tag was clicked');
});
// on each click of the page, you will see: "The <body /> tag was clicked"
@kevinchisholm
kevinchisholm / addEventListener.example-2.js
Created December 20, 2019 12:53
addEventListener Example 2
body.removeEventListener('click');
// Uncaught TypeError: Failed to execute 'removeEventListener' on 'EventTarget': 2 arguments required, but only 1 present
@kevinchisholm
kevinchisholm / addEventListener.example-3.js
Created December 20, 2019 12:55
addEventListener Example 3
body.removeEventListener('click', () => {});
// on each click of the page, you will STILL see: "The <body /> tag was clicked"
@kevinchisholm
kevinchisholm / addEventListener.example-4.js
Last active December 20, 2019 13:16
addEventListener Example 4
function bodyClickHandler () {
console.log('The <body /> tag was clicked');
};
const body = document.getElementsByTagName('body')[0];
body.addEventListener('click', bodyClickHandler);
// on each click of the page, you will see: "The <body /> tag was clicked"