为什么我的简单for循环无法正常运行?

时间:2019-02-12 19:52:06

标签: javascript arrays for-loop

我正在完成一项家庭任务,但我只能坚持其中的一部分。 我希望能够从嵌套数组中的每个数组接收每个第三个项目。

    const movies = [
        [`Harry Potter and the Philosopher's Stone`, 125000000, `dCtFvscYcXQKTNvyyaQr2g2UacJ`, 152, 2001, `Let the Magic Begin`],
        [`Harry Potter and the Chamber of Secrets`, 100000000, `sdEOH0992YZ0QSxgXNIGLq1ToUi`, 161, 2002, `Hogwarts is back in session`],
        [`Harry Potter and the Prisoner of Azkaban`, 130000000, `jUFjMoLh8T2CWzHUSjKCojI5SHu`, 141, 2004, `Something wicked this way comes`],
        [`Harry Potter and the Goblet of Fire`, 150000000, `6sASqcdrEHXxUhA3nFpjrRecPD2`, 157, 2005, `Dark And Difficult Times Lie Ahead`],
        [`Harry Potter and the Order of the Phoenix`, 150000000, `4YnLxYLHhT4UQ8i9jxAXWy46Xuw`, 138, 2007, `Evil Must Be Confronted`],
        [`Harry Potter and the Half-Blood Prince`, 250000000, `bFXys2nhALwDvpkF3dP3Vvdfn8b`, 153, 2009, `Dark Secrets Revealed`],
        [`Harry Potter and the Deathly Hallows: Part 1`, 250000000, `maP4MTfPCeVD2FZbKTLUgriOW4R`, 146, 2010, `One Way… One Fate… One Hero`],
        [`Harry Potter and the Deathly Hallows: Part 2`, 125000000, `fTplI1NCSuEDP4ITLcTps739fcC`, 130, 2011, `It all ends here.`]
    ];

    let filteredArray = []
    let filter = () => {
        for (j = 0; j < movies.length; j++) {
            filteredArray.push(movies[j][2]);
        }
    }

        console.log(filteredArray);

我的计划是获得仅包含第3个项目的过滤数组,因此基本上是从旧的过滤出来的数组。 我尝试使用.filter,但似乎无法理解,因此,如果您的解决方案使用.filter,则非常受欢迎。

我在控制台中的输出只会给我一个空数组,如果我在console.log中使用函数过滤器,它会给我未定义的内容。

4 个答案:

答案 0 :(得分:1)

您应该使用map(将一个数组一个一个一个地转换为另一个数组):

let filteredArray = movies.map(movie => movie[2]);

另外,您似乎误解了filter的含义– filter接受原始数组,检查每个项目,仅保留通过条件的项目。即结果数组将具有原始数组的某些元素(或没有),但保留的元素不会更改。

答案 1 :(得分:0)

您需要致电filter()

const movies = [
  [`Harry Potter and the Philosopher's Stone`, 125000000, `dCtFvscYcXQKTNvyyaQr2g2UacJ`, 152, 2001, `Let the Magic Begin`],
  [`Harry Potter and the Chamber of Secrets`, 100000000, `sdEOH0992YZ0QSxgXNIGLq1ToUi`, 161, 2002, `Hogwarts is back in session`],
  [`Harry Potter and the Prisoner of Azkaban`, 130000000, `jUFjMoLh8T2CWzHUSjKCojI5SHu`, 141, 2004, `Something wicked this way comes`],
  [`Harry Potter and the Goblet of Fire`, 150000000, `6sASqcdrEHXxUhA3nFpjrRecPD2`, 157, 2005, `Dark And Difficult Times Lie Ahead`],
  [`Harry Potter and the Order of the Phoenix`, 150000000, `4YnLxYLHhT4UQ8i9jxAXWy46Xuw`, 138, 2007, `Evil Must Be Confronted`],
  [`Harry Potter and the Half-Blood Prince`, 250000000, `bFXys2nhALwDvpkF3dP3Vvdfn8b`, 153, 2009, `Dark Secrets Revealed`],
  [`Harry Potter and the Deathly Hallows: Part 1`, 250000000, `maP4MTfPCeVD2FZbKTLUgriOW4R`, 146, 2010, `One Way… One Fate… One Hero`],
  [`Harry Potter and the Deathly Hallows: Part 2`, 125000000, `fTplI1NCSuEDP4ITLcTps739fcC`, 130, 2011, `It all ends here.`]
];

let filteredArray = []
let filter = () => {
  for (j = 0; j < movies.length; j++) {
    filteredArray.push(movies[j][2]);
  }
}

filter();
console.log(filteredArray);

答案 2 :(得分:0)

问题在于您实际上并没有在调用filter函数,而只是在定义它。如果在console.log()调用之前添加filter();,则可以快速解决问题。

但是实际上更好的方法是使用数组方法.map()。基本上,.map()会遍历数组中的每个元素,并返回一个新数组,该数组包含您想要的任何内容(在您的情况下,您需要内部数组的第3个元素)。

const movies = ...;
let filteredArray = movies.map(function(movie) {
    return movie[2];
});

答案 3 :(得分:0)

问题是您没有调用var request = new XMLHttpRequest(); request.open('GET', '/admin/blogs/43130421348/articles.json'); request.responseType = 'json'; request.send(); request.onload = function() { var articleList = request.response; var articleArray = articleList.articles; var date = new Date(); var ticks = Math.floor(date.getTime() / 1000); var count = 0; articleArray.forEach(function(entry,index, object){ var metaRequest = new XMLHttpRequest(); metaRequest.open('GET', '/admin/blogs/43130421348/articles/'+ entry.id + '/metafields.json'); metaRequest.responseType = 'json'; metaRequest.send(); console.log(index); metaRequest.onload = function() { var articleMetaObj = metaRequest.response; var articleMetaArr = articleMetaObj.metafields; entry.metafields = articleMetaArr; var eventDate = entry.metafields[0].value; } }); }; 函数,因此该函数从未执行过。

致电filter将解决您的问题。

还请查看filter(),这是您本可以使用的最好的。

map