我知道我已经接近.map了,但是我通过模板文字使用的格式没有通过,所以我认为我使用了错误的方法。我在这里想念什么?谢谢。
我未通过的问题:
"rating should equal [{"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"}]"
我的console.log(评分):
[ '{"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 rating = [];
watchList.map((val, i, arr) => {
rating.push(`{"title":"${val.Title}","rating":"${val.imdbRating}"}`);
});
演示测试对象:
{
"Title": "Avatar",
"Year": "2009",
"Rated": "PG-13",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
"imdbRating": "7.9",
"Response": "True"
}
答案 0 :(得分:1)
似乎期望有一个 objects 数组:
“评分应等于[{” title“:” Inception“,” rating“:” 8.8“},...
但是您拥有的是一个 strings 数组:
['{“ title”:“ Inception”,“ rating”:“ 8.8”}',
请尝试map
ping对象数组(并正确使用map
,而不要像当前一样使用forEach
-map
,< em>从地图函数中返回您想要的新数组中的项):
var rating = watchList.map(val => ({
title: val.Title,
rating: val.imdbRating
}));
或者更简洁地讲解参数:
var rating = watchList.map(({ Title: title, imdbRating: rating }) => ({ title, rating }));