Using map function in Javascript

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.