首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历数组的更好方法

遍历数组的更好方法
EN

Stack Overflow用户
提问于 2022-04-21 20:26:48
回答 3查看 64关注 0票数 0

我对javascript很陌生,有人要求我这样做;任务:根据电影标题和发布日期创建至少由五个对象组成的数组。标题发布日期: Jaws 1975 E.T. 1982心灵1960年IT 1990 Vertigo 1958

然后,创建一个循环,循环遍历数组,在警报框中显示用户输入的一年后发布的电影。例如,如果用户键入‘1978’,那么警报框将只显示E.T.和IT的电影标题。

这是我的方法,但我相信有更好的方法,所以我要求一点帮助!谢谢您:)

代码语言:javascript
复制
movies = ["Vertigo", " Psycho", " Jaws", " E.T", " IT"];
year = ["1958", "1960", "1975", "1982", "1990"];


userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
  alert("The year you have selected is " + userInput);

for (movie in movies) {
  if (userInput == year[0]) {
    alert("Here are a list of movies released after; " + userInput);
    alert(movies); 
    break;
  }
  if (userInput == year[1]) {
    alert("Here are a list of movies released after; " + userInput);
    alert(movies[1] + movies[2] + movies[3] + movies[4]); 
    break;
  }
  if (userInput == year[2]) {
    alert("Here are a list of movies released after; " + userInput);
    alert(movies[2], movies[3], movies[4]); 
    break;
  }
  if (userInput == year[3]) {
    alert("Here are a list of movies released after; " + userInput);
    alert(movies[3], movies[4]); 
    break;
  }
  if (userInput == year[4]) {
    alert("Here are a list of movies released after; " + userInput);
    alert(movies[4]); 
    break;
  }
  else {
    alert("There are no movies. Please input the correct information and try again.");
    break;
  }
}
}
EN

回答 3

Stack Overflow用户

发布于 2022-04-21 20:48:12

基本上,您做了一个硬代码,包装了所有这些年和电影的名称在obj和循环在每个键和值。

代码语言:javascript
复制
movies = {
    1958 : "Vertigo",
    1960 : " Psycho",
    1975 : " Jaws",
    1982 : " E.T",
    1990 : " IT",
}

userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
alert("The year you have selected is " + userInput);

Object.entries(movies).forEach(
    ([key, value]) => key < userInput &&  alert("Here are a list of movies released after; " + value));
票数 0
EN

Stack Overflow用户

发布于 2022-04-21 20:52:12

在不修改数据结构的情况下,这是一个好方法:

首先,根据用户的输入过滤电影:

代码语言:javascript
复制
const filteredYears = year.filter(item => Number(item) >= Number(userInput);

然后得到每一年过滤的电影。

代码语言:javascript
复制
// Loop the filtered years
filteredYears.forEach(year => {
    // Get the index of current element
    const index = year.findIndex(y => y === year);

    // Just in case, if year found
    if ( index !== -1 ){
        alert("Here are a list of movies released after; " + userInput);
        // The movie of this year.
        alert(movies[index]);
    } 
});
票数 0
EN

Stack Overflow用户

发布于 2022-04-21 20:57:06

试着使用电影的对象:

代码语言:javascript
复制
movies = [
  {
    title:"Vertigo",
    year:1958
  },
   {
    title:"Psycho",
    year:1960
  },
   {
    title:"Jaws",
    year:1975
  }, {
    title:"E.T",
    year:1982
  }, {
    title:"IT",
    year:1990
  }
]

userInput = prompt("Enter the year of movies you are interested in viewing\nYear: ");
alert("The year you have selected is " + userInput);

//filter movies by year from userInput
const selectedMovies=movies.filter((movie)=>movie.year>=parseInt(userInput));

//check if any movie in array
if(selectedMovies.length){
  alert("Here are a list of movies released after: " + userInput);
  //get only movies titles to array
  const moviesTitlesToDisplay=selectedMovies.reduce((list,movie)=> list.concat(movie.title),[]);
  alert(moviesTitlesToDisplay);
} else {
  alert("There are no movies. Please input the correct information and try again.");
}

CodePen示例:https://codepen.io/marcinbzone/pen/popYBaJ

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71960513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档