Reputation: 355
I'm working on a home task but i'm stuck at one part. I want to be able to receive every 3rd item from every array in the nested array.
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);
My plan was to get a filtered array with only the 3rd items, so basically a filtered array from the old one. I tried using .filter but i can't seem to understand how, so if your solution uses .filter, it's very welcome.
My output in the console just gives me an empty array, and if i console.log the function filter, it gives me undefined.
Upvotes: -1
Views: 62
Reputation: 5615
The problem is you're not actually calling your filter
function, you're only defining it. If you add filter();
before your console.log() call, that's a quick fix.
But really the better way is to use the array method .map()
. Basically .map() loops over each element in an array, returning a new array of whatever you want (in your case you want the 3rd element of the inner arrays).
const movies = ...;
let filteredArray = movies.map(function(movie) {
return movie[2];
});
Upvotes: 0
Reputation: 3731
the problem is that you are not calling your filter
function, thus it is never been executed.
calling filter()
will solve your problem.
also take a look into map
which is the best you could have used.
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.`]
];
//quickest way.
let filteredArray = movies.map( innerArray => innerArray[2]);
//using for each
let filteredArrayFor =[];
let filter = () => {
for (let j = 0; j < movies.length; j++) {
filteredArrayFor.push(movies[j][2]);
}
}
console.log(filteredArray);
filter();
console.log(filteredArrayFor);
Upvotes: 0
Reputation: 7981
You should use a map
(converting one array into another, element by element):
let filteredArray = movies.map(movie => movie[2]);
Additionally you seem to misunderstand what filter
means – a filter
takes the original array, checks each item, and only keeps the ones that pass a condition. I.e. the resulting array will have some (or none) of the original array's elements, but the ones that were kept are not changed.
Upvotes: 1
Reputation: 44135
You need to call with 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);
Upvotes: 0