Javascript Array filter() convert the array of objects

Introduction

Use the filter() method combined with the map() method to convert the array of objects

var movies = [
   {id: 1, title: "CSS", rating: 4.0},
   {id: 2, title: "HTML", rating: 5.0},
   {id: 3, title: "Java", rating: 3.0},
   {id: 4, title: "Javascript", rating: 2.0}
];

into an array of objects that only contain movie ids of the movies with the letter 'b' in the title (case insensitive).

The result should look like:

[3, 4]

var movies = [/*from  www  . ja  v  a2 s. c  o m*/
  {id: 1, title: "CSS", rating: 4.0},
  {id: 2, title: "HTML", rating: 5.0},
  {id: 3, title: "Java", rating: 3.0},
  {id: 4, title: "Javascript", rating: 2.0}
];

var bMovies = movies.filter(function(movie) {
  return movie.title.toLowerCase().indexOf("j") !== -1;
}).map(function(movie) { return movie.id });

console.log(bMovies);



PreviousNext

Related