编写一个函数,该函数可以将任意数量的剧集 ID 作为输入并返回有关这些剧集的所有信息。
在将用户输入为多个 id 号后,它不会将输出作为所有剧集的信息
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 id = prompt('Enter the episode ids');
let Info = (...id) => {
for(let current in BigBang._embedded.episodes) {
if(BigBang._embedded.episodes[current].id === parseInt(id)) {
let Detail = BigBang._embedded.episodes[current];
console.log(Detail);
}
}
}
Info(id);
在将用户输入为多个 id 号后,它不会将输出作为所有剧集的信息
假设您需要传递多个id,您可以将函数filter
与函数一起使用indexOf
(这是转换为数字并进行比较,当然您可以先映射该数组等。但那部分取决于您)。
这个例子接收以逗号分隔的id并Info
使用函数调用函数apply
,将输入的id作为参数传递(就像我说的这是一个例子,在实际执行中你会直接将参数传递给函数Info
)。
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 id = prompt('Enter the episode ids (separated by comma)');
let Info = (...ids) => {
var episodes = BigBang._embedded.episodes.filter(e => ids.findIndex(i => e.id == +i) !== -1);
console.log(episodes);
}
Info.apply(null, id.split(','));
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句