gpt4 book ai didi

javascript - 使用 .map FCC 格式化对象查询

转载 作者:行者123 更新时间:2023-12-03 01:37:10 24 4
gpt4 key购买 nike

我知道我很接近 .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"
}

最佳答案

它看起来需要一个对象数组:

"rating should equal [{"title":"Inception","rating":"8.8"}, ...

但是你拥有的是一个字符串数组:

[ '{"title":"Inception","rating":"8.8"}',

尝试mapping到对象数组(并正确使用map,而不是像您当前正在做的那样作为forEach - 到map从map函数返回您想要在新数组中出现的项目):

var rating = watchList.map(val => ({
title: val.Title,
rating: val.imdbRating
}));

或者,通过参数解构更简洁:

var rating = watchList.map(({ Title: title, imdbRating: rating }) => ({ title, rating }));

关于javascript - 使用 .map FCC 格式化对象查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com