Using map function in Javascript

2019 January, 15th

Arrays are collections of data which at times have to be manipulated by the programmer.

Suppose you are working as a software developer for a company such as Spotify and one day your manager asked you to extract only the names of the artists from a list of songs.


How might you solve this challenge?

This is how the internal collection of songs (music) might look like at Spotify.


1const songs = [
2 {
3 id: 1,
4 timesListened: 1,
5 title: "Doin' Time",
6 artist: "Lana Del Rey",
7 },
8 {
9 id: 2,
10 timesListened: 4,
11 title: "somethingfartoogoodtofeel",
12 artist: "The Japanese House",
13 },
14 {
15 id: 3,
16 timesListened: 2,
17 title: "Eastside",
18 artist: "Benny Blanco, Halsey, Khalid",
19 },
20 {
21 id: 4,
22 timesListened: 10,
23 title: "Everybody Hurts",
24 artist: "Jasmine Thompson",
25 },
26 {
27 id: 5,
28 timesListened: 3,
29 title: "Team",
30 artist: "Lorde",
31 },
32]

The wonders of the map() function now is as follows:


Using map with arrow functions.
1const artists = songs.map(song => song.artist)

An alternative way of doing this:

1const artists = songs.map(function(song) {
2 return song.artist
3})

Challenge

How would you extract only the names of songs that have been played more than 3 times?

Newsletter

Get the latest posts for in your inbox, every week. We make it super easy to keep up with some interesting discoveries and challenges of working developers along the week.

We've used Linode, a web hosting platform, for both this site and other projects for the last 15 years. It does not disappoint!

Simplify your cloud infrastructure with their Linux virtual machines and robust set of tools to develop, deploy, and scale your modern applications faster and easier.

Check them out!
Who are we?

Coder in 90 provides developers with bite sized courses for thousands of developers around the world.

Β© 2014 - 2020 Coder in 90
a Smartywak service

Built with ❀ from πŸ‡ͺπŸ‡ͺπŸ‡ͺπŸ‡ΈπŸ‡ΈπŸ‡ͺ