编写一个将季节编号和剧集编号作为输入的功能 并提供有关该特定剧集的所有信息作为输出
在将用户的输入作为季节编号和剧集编号后,它不会将输出作为有关该特定剧集的信息
let BigBang = {
"_embedded": {
"episodes": [
{
"id": 2913,
"name": "Pilot",
"season": 1,
"number": 1,
"airdate": "2007-09-24",
"airtime": "20:30",
"airstamp": "2007-09-25T00:30:00+00:00",
"runtime": 30,
"_links": {
"self": {
"href": "http:\/\/api.tvmaze.com\/episodes\/2913"
}
}
},
{
"id": 2914,
"name": "The Big Bran Hypothesis",
"season": 1,
"number": 2,
"airdate": "2007-10-01",
"airtime": "20:30",
"airstamp": "2007-10-02T00:30:00+00:00",
"runtime": 30,
"image": {
"medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
"original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
},
}
let season = prompt('Enter Season number');
let number = prompt('Enter Episode number');
let AllInfo = (season,number) => {
for(let current in BigBang._embedded.episodes) {
if(BigBang._embedded.episodes[current].season === season) {
if(BigBang._embedded.episodes[current].number === number) {
let Detail = BigBang._embedded.episodes[current];
alert(Detail);
}
}
}
AllInfo(season,number);
}
答案 0 :(得分:1)
尝试使用.find
代替它,它会使代码更清晰:
let BigBang = {
"_embedded": {
"episodes": [{
"id": 2913,
"name": "Pilot",
"season": 1,
"number": 1,
"airdate": "2007-09-24",
"airtime": "20:30",
"airstamp": "2007-09-25T00:30:00+00:00",
"runtime": 30,
"_links": {
"self": {
"href": "http:\/\/api.tvmaze.com\/episodes\/2913"
}
}
},
{
"id": 2914,
"name": "The Big Bran Hypothesis",
"season": 1,
"number": 2,
"airdate": "2007-10-01",
"airtime": "20:30",
"airstamp": "2007-10-02T00:30:00+00:00",
"runtime": 30,
"image": {
"medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
"original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
},
}
]
}
}
//const inputSeason = prompt('Enter Season number');
const inputSeason = 1;
//const inputNumber = prompt('Enter Episode number');
const inputNumber = 2;
const foundEpisode = BigBang._embedded.episodes.find(({ season, number}) => {
return season === inputSeason && number === inputNumber;
});
if (foundEpisode) console.log(foundEpisode);
else console.log('No matching season/number found!');
答案 1 :(得分:0)
您可以使用阵列上的find()方法更轻松地完成此任务。
let episode = BigBang._embedded.episodes.find((e) => {
return e.season === season && e.number === number;
});
if (episode) {
alert(episode.name);
}
答案 2 :(得分:0)
我调试了你的代码,发现你在 AllInfo 中调用了 AllInfo 函数。所以你的代码中会发生递归调用。因此,从AllInfo函数中删除 AllInfo 的调用,您的问题将得到解决。请尝试以下代码。
let BigBang = {
"_embedded": {
"episodes": [
{
"id": 2913,
"name": "Pilot",
"season": 1,
"number": 1,
"airdate": "2007-09-24",
"airtime": "20:30",
"airstamp": "2007-09-25T00:30:00+00:00",
"runtime": 30,
"_links": {
"self": {
"href": "http:\/\/api.tvmaze.com\/episodes\/2913"
}
}
},
{
"id": 2914,
"name": "The Big Bran Hypothesis",
"season": 1,
"number": 2,
"airdate": "2007-10-01",
"airtime": "20:30",
"airstamp": "2007-10-02T00:30:00+00:00",
"runtime": 30,
"image": {
"medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
"original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
},
}]}};
let season = 1;
let number = 2;
let AllInfo = (season,number) => {
for(let current in BigBang._embedded.episodes) {
if(BigBang._embedded.episodes[current].season === season) {
if(BigBang._embedded.episodes[current].number === number) {
let Detail = BigBang._embedded.episodes[current];
alert(JSON.stringify(Detail,null,4));
}
}
}
}
AllInfo(season,number);