Javascript 如何在不使用箭头函数的情况下编写此函数?

Javascript 如何在不使用箭头函数的情况下编写此函数?,javascript,arrays,object,ecmascript-6,functional-programming,Javascript,Arrays,Object,Ecmascript 6,Functional Programming,这是一门课程的练习。给定一个对象数组,我必须使用map方法循环所有元素,并返回一个名为“rating”的新变量,这个新数组只包含每个对象的Title和rating属性。给定的OBEJCT数组如下所示: var watchList = [ { "Title": "Inception", "Year": "2010", "Rated": "PG-13",

这是一门课程的练习。给定一个对象数组,我必须使用map方法循环所有元素,并返回一个名为“rating”的新变量,这个新数组只包含每个对象的Title和rating属性。给定的OBEJCT数组如下所示:

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"
            },
            {
               "Title": "The Dark Knight",
               "Year": "2008",
               "Rated": "PG-13",
               "Released": "18 Jul 2008",
               "Runtime": "152 min",
               "Genre": "Action, Adventure, Crime",
               "Director": "Christopher Nolan",
               "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
               "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
               "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
               "Language": "English, Mandarin",
               "Country": "USA, UK",
               "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
               "Metascore": "82",
               "imdbRating": "9.0",
               "imdbVotes": "1,652,832",
               "imdbID": "tt0468569",
               "Type": "movie",
               "Response": "True"
            },
            {  
               "Title": "Batman Begins",
               "Year": "2005",
               "Rated": "PG-13",
               "Released": "15 Jun 2005",
               "Runtime": "140 min",
               "Genre": "Action, Adventure",
               "Director": "Christopher Nolan",
               "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
               "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
               "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
               "Language": "English, Urdu, Mandarin",
               "Country": "USA, UK",
               "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
               "Metascore": "70",
               "imdbRating": "8.3",
               "imdbVotes": "972,584",
               "imdbID": "tt0372784",
               "Type": "movie",
               "Response": "True"
            },
            {
               "Title": "Avatar",
               "Year": "2009",
               "Rated": "PG-13",
               "Released": "18 Dec 2009",
               "Runtime": "162 min",
               "Genre": "Action, Adventure, Fantasy",
               "Director": "James Cameron",
               "Writer": "James Cameron",
               "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
               "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
               "Language": "English, Spanish",
               "Country": "USA, UK",
               "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
               "Metascore": "83",
               "imdbRating": "7.9",
               "imdbVotes": "876,575",
               "imdbID": "tt0499549",
               "Type": "movie",
               "Response": "True"
            }];
rating = watchList.map( (item) => ({"title":item["Title"], "rating":item["imdbRating"]}) );
最终结果如下:

[{"title":"Inception","rating":"8.8"},{"title":"Interstellar","rating":"8.6"},{"title":"The Dark Knight","rating":"9.0"},{"title":"Batman Begins","rating":"8.3"},{"title":"Avatar","rating":"7.9"}]
老实说,由于我无法真正想出解决方案,我寻找了它并找到了这个:

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"
            },
            {
               "Title": "The Dark Knight",
               "Year": "2008",
               "Rated": "PG-13",
               "Released": "18 Jul 2008",
               "Runtime": "152 min",
               "Genre": "Action, Adventure, Crime",
               "Director": "Christopher Nolan",
               "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
               "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
               "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
               "Language": "English, Mandarin",
               "Country": "USA, UK",
               "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
               "Metascore": "82",
               "imdbRating": "9.0",
               "imdbVotes": "1,652,832",
               "imdbID": "tt0468569",
               "Type": "movie",
               "Response": "True"
            },
            {  
               "Title": "Batman Begins",
               "Year": "2005",
               "Rated": "PG-13",
               "Released": "15 Jun 2005",
               "Runtime": "140 min",
               "Genre": "Action, Adventure",
               "Director": "Christopher Nolan",
               "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
               "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
               "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
               "Language": "English, Urdu, Mandarin",
               "Country": "USA, UK",
               "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
               "Metascore": "70",
               "imdbRating": "8.3",
               "imdbVotes": "972,584",
               "imdbID": "tt0372784",
               "Type": "movie",
               "Response": "True"
            },
            {
               "Title": "Avatar",
               "Year": "2009",
               "Rated": "PG-13",
               "Released": "18 Dec 2009",
               "Runtime": "162 min",
               "Genre": "Action, Adventure, Fantasy",
               "Director": "James Cameron",
               "Writer": "James Cameron",
               "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
               "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
               "Language": "English, Spanish",
               "Country": "USA, UK",
               "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
               "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
               "Metascore": "83",
               "imdbRating": "7.9",
               "imdbVotes": "876,575",
               "imdbID": "tt0499549",
               "Type": "movie",
               "Response": "True"
            }];
rating = watchList.map( (item) => ({"title":item["Title"], "rating":item["imdbRating"]}) );
但是,这是一个箭头函数,我只了解了一部分,所以我尝试使用ECMA5规则来解决它,我尝试了以下方法:

var rating = watchList.map(function (item){
let obj ={
   "title":item["Title"], 
    "rating":item["imdbRating"]
    }
    return obj;
}))

我的观点是,对于map函数循环的每个元素,它都会在新数组中返回一个对象,但在控制台上显示的结果是:

[object Object],[object Object],[object Object],[object Object],[object Object]
那么,如何在不使用箭头函数的情况下编写函数并获得准确的结果呢


提前感谢您

它工作得非常好-请参阅我如何将arrow函数修改为ES5兼容函数:

var监视列表=[{
“标题”:“盗梦空间”,
“年份”:“2010年”,
“额定”:“PG-13”,
“发布”:“2010年7月16日”,
“运行时间”:“148分钟”,
“类型”:“动作、冒险、犯罪”,
“导演”:“克里斯托弗·诺兰”,
“作家”:“克里斯托弗·诺兰”,
“演员”:“莱昂纳多·迪卡普里奥、约瑟夫·戈登·莱维特、艾伦·佩奇、汤姆·哈代”,
“阴谋”:“通过分享梦想技术窃取公司秘密的窃贼,被赋予相反的任务,将想法植入CEO的头脑。”,
“语言”:“英语、日语、法语”,
“国家”:“美国、英国”,
“奖项”:“获得4项奥斯卡奖,另外143项获奖,198项提名。”,
“海报”:http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg“,
“元分数”:“74”,
“imdbRating”:“8.8”,
“IMDBVOUTES”:“1446708”,
“imdbID”:“tt1375666”,
“类型”:“电影”,
“响应”:“正确”
},
{
“标题”:“星际”,
“年份”:“2014年”,
“额定”:“PG-13”,
“发布”:“2014年11月7日”,
“运行时间”:“169分钟”,
“类型”:“冒险、戏剧、科幻”,
“导演”:“克里斯托弗·诺兰”,
“作家”:“乔纳森·诺兰,克里斯托弗·诺兰”,
“演员”:“艾伦·伯斯廷、马修·麦康纳、麦肯齐·福伊、约翰·利思高”,
“情节”:“一队探险家穿越太空虫洞,试图确保人类的生存。”,
“语言”:“英语”,
“国家”:“美国、英国”,
“奖项”:“获得1项奥斯卡奖。另外39项获奖和132项提名。”,
“海报”:http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg“,
“元分数”:“74”,
“imdbRating”:“8.6”,
“IMDBVOUTES”:“910366”,
“imdbID”:“tt0816692”,
“类型”:“电影”,
“响应”:“正确”
},
{
“头衔”:“黑暗骑士”,
“年”:“2008年”,
“额定”:“PG-13”,
“发布”:“2008年7月18日”,
“运行时间”:“152分钟”,
“类型”:“动作、冒险、犯罪”,
“导演”:“克里斯托弗·诺兰”,
“编剧”:“乔纳森·诺兰(剧本)、克里斯托弗·诺兰(剧本)、克里斯托弗·诺兰(故事)、大卫·S·戈耶(故事)、鲍勃·凯恩(角色)”,
“演员”:“克里斯蒂安·贝尔、希思·莱杰、亚伦·埃克哈特、迈克尔·凯恩”,
“阴谋”:“当被称为小丑的威胁对高谭人民造成浩劫和混乱时,披着斗篷的十字军战士必须接受对其与不公正作斗争能力的最大心理测试之一。”,
“语言”:“英语、普通话”,
“国家”:“美国、英国”,
“奖项”:“获得2项奥斯卡奖,另外146项和142项提名。”,
“海报”:http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg“,
“元分数”:“82”,
“imdbRating”:“9.0”,
“imdbvoces”:“1652832”,
“imdbID”:“tt0468569”,
“类型”:“电影”,
“响应”:“正确”
},
{
“标题”:“蝙蝠侠开始”,
“年份”:“2005年”,
“额定”:“PG-13”,
“发布”:“2005年6月15日”,
“运行时间”:“140分钟”,
“类型”:“动作、冒险”,
“导演”:“克里斯托弗·诺兰”,
“作家”:“鲍勃·凯恩(角色)、大卫·S·高耶(故事)、克里斯托弗·诺兰(剧本)、大卫·S·高耶(剧本)”,
“演员”:“克里斯蒂安·贝尔、迈克尔·凯恩、利亚姆·尼森、凯蒂·赫尔姆斯”,
“阴谋”:“在与导师一起训练后,蝙蝠侠开始了他的战斗,以使犯罪猖獗的高谭市摆脱稻草人和阴影联盟对其造成的腐败。”,
“语言”:“英语、乌尔都语、普通话”,
“国家”:“美国、英国”,
“奖项”:“获得1项奥斯卡提名,另外15项获奖,66项提名。”,
“海报”:http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
“元分数”:“70”,
“imdbRating”:“8.3”,
“IMDBVOUTES”:“972584”,
“imdbID”:“tt0372784”,
“类型”:“电影”,
“响应”:“正确”
},
{
“标题”:“阿凡达”,
“年份”:“2009年”,
“额定”:“PG-13”,
“发布”:“2009年12月18日”,
“运行时间”:“162分钟”,
“类型”:“动作、冒险、幻想”,
“导演”:“詹姆斯·卡梅隆”,
“作家”:“詹姆斯·卡梅隆”,
“演员”:“萨姆·沃辛顿、佐伊·萨尔达纳、西格尼·韦弗、斯蒂芬·朗”,
“阴谋”:“一名瘫痪的海军陆战队员被派往潘多拉月球执行一项独特的任务,他在服从命令和保护他认为是自己家的世界之间左右为难。”,
“语言”:“英语、西班牙语”,
“国家”:“美国、英国”,
“奖项”:“获得3项奥斯卡奖,另外80项和121项提名。”,
“海报”:http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg“,
“元分数”:“83”,
“imdbRating”:“7.9”,
“IMDBVOUTES”:“876575”,
“imdbID”:“tt0499549”,
“类型”:“电影”,
“响应”:“正确”
}
];
var ratings=watchList.map(功能(项目){
返回{“Title”:item[“Title”],“Rating”:item[“imdbRating”]}
});

控制台日志(评级)
问题可能在于您使用console.log的方式(与对象打印的字符串连接
[object object]

两者都