现代javascript rest函数为n个传递的参数提供n个输出

时间:2018-03-31 10:59:05

标签: javascript ecmascript-6 parameter-passing

我正在尝试将多个剧集ID作为参数,并从json数据中显示该剧集的信息。 我已将episodeArray中的所有剧集信息推送完毕 但是显示它时,两个参数显示输出数组两次。 我如何只展示一次?

JAVACRIPT CODE

我的代码是:

let episodeData = bigEpisodeData._embedded.episodes

let searchEpisode = (...epiId) => {
  let episodeArray = new Array()
  for (currId of epiId){
    for (currentEpisode of episodeData) {
      let epiInfo = currentEpisode
      if (epiInfo.id === currId) {
        episodeArray.push(epiInfo)
        console.log(episodeArray)
      } 
    }
  }
  }


searchEpisode(3061, 3062)

it is showing the output array two time

1 个答案:

答案 0 :(得分:0)

您需要将console.log移到循环之外



let searchEpisode = (...epiId) => {
  let episodeArray = new Array()
  for (currId of epiId) {
    for (currentEpisode of episodeData) {
      let epiInfo = currentEpisode
      if (epiInfo.id === currId) {
        episodeArray.push(epiInfo)
      }
    }
  }
  console.log(episodeArray)
}