freeCodeCamp Challenge Guide: Use the map Method to Extract Data from an Array

Use the map Method to Extract Data from an Array


Hints

Hint 1

array.prototype.map takes a function as in input and returns an array. The returned array includes elements that is processed by the function. This function takes individual elements as input.


Solutions

Solution 1 (Click to Show/Hide)
const ratings = watchList.map(item => ({
  title: item["Title"],
  rating: item["imdbRating"]
}));

Code Explanation

Using ES6 notation, each item in array is processed to extract title and rating.
Parantheses are needed to return an object.

Solution 2 (Click to Show/Hide)
const ratings = watchList.map(({ Title: title, imdbRating: rating }) => ({title, rating}));

Code Explanation

Using parameter destructuring, the title and rating are extracted and returned in an object. Parantheses are needed to return an object.


Relevant Links

107 Likes

Hello campers,here is how i did it:

var oldArray = [1,2,3,4,5];

// Only change code below this line.

var newArray = oldArray.map(function(val) {
return val + 3;
});

console.log(oldArray);
console.log(newArray);

13 Likes

Hey this should work but it is not passing all tests!

var oldArray = [1,2,3,4,5];

// Only change code below this line.
var newArray = Array.prototype.map.call(oldArray,function(i,v){
return i+3;
});

2 Likes