Javascript Array filter() filter object by attributes

Introduction

Use the filter() method with the following 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}
 ];
to create a new array of objects that only contain movies with a rating less than 4.0.

var movies = [/*from   www  .  j a va 2 s  . com*/
  {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 terribleMovies = movies.filter(function(movie) {
  return movie.rating < 4.0;
});

console.log(terribleMovies);



PreviousNext

Related