此功能性Javascript示例如何工作?

时间:2019-01-22 04:32:30

标签: javascript arrays function object functional-programming

我似乎无法把这个代码片段包裹住。您能给我一个或两个提示吗?

  var filteredList = watchList.map(function(e) {
  return {title: e["Title"], rating: e["imdbRating"]}
  }).filter((e) => e.rating >= 8);

问题:我知道'e'是传递给map方法的回调的参数,但是e [“ Title”]和e [“ imdbRating”]是什么?我们期望在对象数组上运行此功能。我不了解语法,甚至不知道如何调用某些语法。十分困惑。

我了解此代码的用途,但是我们如何使用此title: e["Title"], rating: e["imdbRating"] ???

这是一个对象阵列的示例!

var watchList = [
                 {  
                   "Title": "Inception",
                   "Year": "2010",
                   "Rated": "PG-13",
                   "Released": "16 Jul 2010",
                   "Runtime": "148 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Christopher Nolan",
                   "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
                   "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
                   "Language": "English, Japanese, French",
                   "Country": "USA, UK",
                   "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.8",
                   "imdbVotes": "1,446,708",
                   "imdbID": "tt1375666",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Interstellar",
                   "Year": "2014",
                   "Rated": "PG-13",
                   "Released": "07 Nov 2014",
                   "Runtime": "169 min",
                   "Genre": "Adventure, Drama, Sci-Fi",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan, Christopher Nolan",
                   "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
                   "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
                   "Language": "English",
                   "Country": "USA, UK",
                   "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.6",
                   "imdbVotes": "910,366",
                   "imdbID": "tt0816692",
                   "Type": "movie",
                   "Response": "True"
                },

4 个答案:

答案 0 :(得分:2)

e是具有某些属性的对象。想象一下它是这样的:

var e = {
    Title: 'foo',
    imdbRating: 7.2,
};

因此e [“ Title”]将返回'foo',而e [“ imdbRating”]将返回7.2。

您发布的函数也可以这样写:

  var filteredList = watchList.map(function(e) {
      return {title: e.Title, rating: e.imdbRating}
  }).filter((e) => e.rating >= 8);

也许这使它更容易理解。

答案 1 :(得分:2)

此处e指向数组中正在处理的当前元素。因此e将基本上代表数组中的每个对象。您可以将e替换为任何其他有效名称。

在您的代码中,首先map创建一个新的对象数组,每个对象都有tw键titleimbRating,然后再次对其应用过滤器以创建另一个新数组,其中imbRating的值大于8

var watchList = [{
    "Title": "Inception",
    "Year": "2010",
    "Rated": "PG-13",
    "Released": "16 Jul 2010",
    "Runtime": "148 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Christopher Nolan",
    "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
    "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
    "Language": "English, Japanese, French",
    "Country": "USA, UK",
    "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.8",
    "imdbVotes": "1,446,708",
    "imdbID": "tt1375666",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Interstellar",
    "Year": "2014",
    "Rated": "PG-13",
    "Released": "07 Nov 2014",
    "Runtime": "169 min",
    "Genre": "Adventure, Drama, Sci-Fi",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan, Christopher Nolan",
    "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
    "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
    "Language": "English",
    "Country": "USA, UK",
    "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.6",
    "imdbVotes": "910,366",
    "imdbID": "tt0816692",
    "Type": "movie",
    "Response": "True"
  }
]



var filteredList = watchList.map(function(e) {
  return {
    title: e["Title"],
    rating: e["imdbRating"]
  }
}).filter((e) => e.rating >= 8);

console.log(filteredList)

答案 2 :(得分:1)

var filteredList = watchList.map(function(e) {
  return {title: e["Title"], rating: e["imdbRating"]}
  }).filter((e) => e.rating >= 8);

在上面的代码中,map函数用于迭代watchList数组中的所有元素。 Map逐一迭代所有对象的值。 e被分配了对象。它返回一个对象,该对象的属性为e["Title"]。这是访问对象属性的一种方式。e["Title"]e.imdbRating将分别调用与titleimdbRating值相关的值。

答案 3 :(得分:0)

我们将其拆分,这样更有意义

首先,我们使用map创建一个新数组。 Map通过遍历array(在这种情况下为watchList)进行工作,在每次迭代时,它将当前数组元素传递给提供的回调函数,返回值定义了新数组中索引i处的值,其中索引i是传递的元素的位置到回调函数。

例如:

const timeTwo = [1, 2, 3].map(num => num * 2);
// timesTwo = [2, 4, 6]

在您提供的示例中,地图返回了一个新的对象数组,其中包含属性标题和等级。标题与原始数组相同,但等级是e.imdbRating的映射值。

var filteredList = watchList.map(function(e){
  return {
    title: e.title,
    rating: e.imdbRating,
  };
});

现在让我们看下一部分。过滤器通过遍历旧数组来创建新数组,如果传递给过滤器的回调返回true,则该元素被添加到新数组;如果返回false,则排除该元素。

在这种情况下,过滤列表的最终值将是具有标题和等级属性的对象数组,其中等级大于或等于8;

  var filteredList = filteredList.filter((e) => e.rating >= 8);

让它们放在一起,watchList.map返回一个新数组,并且所有数组都有一个filter方法,因此我们可以链式过滤掉map(如下图所示)。最后过滤,返回一个数组,该数组将存储在变量filteredList中。

  var filteredList = watchList.map(function(e) {
      return {title: e.Title, rating: e.imdbRating}
  }).filter((e) => e.rating >= 8);
相关问题