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
// 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 => { |
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
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 }, |
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
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); | |
}, []); |
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
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); | |
}, {}); |
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
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); |
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
// 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"; | |
}); |
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
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"); |
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
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 | |
}); |
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
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", | |
}); |
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
const lowerCaseSongs = songs.map(mySongFunc); | |
var mySongFunc = function(song) { | |
return song.name.toLowerCase(); | |
}; | |
// ES6 | |
const mySongFunc = song => { | |
return song.name.toLowerCase(); | |
}; |
NewerOlder