Skip to content

Instantly share code, notes, and snippets.

View JoaoCnh's full-sized avatar
🎯
Focusing

João Cunha JoaoCnh

🎯
Focusing
View GitHub Profile
@JoaoCnh
JoaoCnh / triforce.js
Created February 14, 2018 15:18
Solving the problem
// Let's reduce the array of arrays into a single one
const songNames = allSongs.reduce((acc, currValue) => {
return acc.concat(currValue);
}, [])
// Let's map it out with the seconds turned into minutes
.map(song => {
return { ...song, duration: Math.floor(song.duration / 60) };
})
// Let's filter the ones under 3 minutes
.filter(song => {
@JoaoCnh
JoaoCnh / songs.js
Last active October 14, 2018 09:27
complex songs
const spotifySongs = [
{ id: 1, name: "Curl of the Burl", artist: "Mastodon", duration: 204 },
{ id: 2, name: "Oblivion", artist: "Mastodon", duration: 306 },
{ id: 3, name: "Flying Whales", artist: "Gojira", duration: 444 },
{ id: 4, name: "L'Enfant Sauvage", artist: "Gojira", duration: 246 }
];
const lastFmSongs = [
{ id: 5, name: "Chop Suey", artist: "System of a Down", duration: 192 },
{ id: 6, name: "Throne", artist: "Bring me the Horizon", duration: 186 },
@JoaoCnh
JoaoCnh / flatarray.js
Created February 14, 2018 14:55
Array of Arrays flatten with reduce
const mult = [songs, [{id: 112, name: "Chop Suey", artist: "System of a Down" }]];
var flatMult = mult.reduce(function (acc, currValue) {
return acc.concat(currValue);
}, []);
// ES6
const flatMult = mult.reduce((acc, currValue) => {
return acc.concat(currValue);
}, []);
@JoaoCnh
JoaoCnh / obj.js
Last active October 14, 2018 09:27
Reduce object from array
var obj = songs.reduce(function (acc, currValue) {
var artist = currValue.artist;
var artistCount = acc[artist] ? acc[artist] + 1 : 1;
var newObj = {};
newObj[artist] = artistCount;
return Object.assign(acc, newObj);
}, {});
@JoaoCnh
JoaoCnh / sum.js
Created February 14, 2018 14:38
Reduce example - sum
const numbers = [2,10,11,5,16];
var sum = numbers.reduce(function (acc, currValue) {
return acc + currValue;
}, 0);
// ES6
const sum = numbers.reduce((acc, currValue) => {
return acc + currValue;
}, 0);
@JoaoCnh
JoaoCnh / filter3.js
Created February 14, 2018 11:10
Filter example 3
// using the songs array from previous examples
var mastodonSongs = songs.filter(function (song) {
return song.artist.toLowerCase() === "mastodon";
});
// ES6
const mastodonSongs = songs.filter(song => {
return song.artist.toLowerCase() === "mastodon";
});
@JoaoCnh
JoaoCnh / filter2.js
Created February 14, 2018 11:06
Filter example 2
var strings = ["hello", "Matt", "Mastodon", "Cat", "Dog"];
var filtered = strings.filter(function (str) {
return str.includes("at");
});
// ES6
const filtered = strings.filter(str => {
return str.includes("at");
@JoaoCnh
JoaoCnh / filter.js
Created February 14, 2018 11:03
Filter example 1
var numbers = [1,2,3,4,5,6,7,8,9,10];
var evenNumbers = numbers.filter(function (num) {
return num % 2 === 0;
});
// ES6
const evenNumbers = numbers.filter(num => {
return num % 2 === 0
});
@JoaoCnh
JoaoCnh / map3.js
Created February 14, 2018 10:03
javascript map 3rd example
var mapped = songs.map(function (song) {
// using _.omit we remove the artist property
// don't use delete because it mutates the song object
// while _.omit creates a new one
var newSong = _.omit(song, "artist");
return Object.assign(newSong, {
scrobbleCount: 0,
spotifyUrl: "let's just imagine these properties make sense for now",
});
@JoaoCnh
JoaoCnh / map2.js
Created February 14, 2018 09:29
javascript map 2nd example
const lowerCaseSongs = songs.map(mySongFunc);
var mySongFunc = function(song) {
return song.name.toLowerCase();
};
// ES6
const mySongFunc = song => {
return song.name.toLowerCase();
};