迭代Javascript对象

时间:2018-03-28 14:55:12

标签: javascript

编写一个可以将任意数量的剧集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);

2 个答案:

答案 0 :(得分:1)

let str = prompt('Enter the episode ids');
let idArray = JSON.parse("[" + str + "]");
let Info = (ids) => BigBang._embedded.episodes.filter(b => ids.includes(b.id));
console.log(Info(idArray));

答案 1 :(得分:0)

  

在将来自用户的输入作为多个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; }