Reputation: 225
I have the following array, which contains a series of objects. Inside of each object is a property with an array.
const films = [
{
name: 'Ant-Man and the Wasp',
genre: ['Action' , 'Adventure' , 'Sci-Fi' , 'Comedy']
},
{
name: 'Sorry to Bother You',
genre: ['Comedy' , 'Fantasy']
},
{
name: 'Jurassic World: Fallen Kingdom',
genre: ['Action' , 'Adventure' , 'Sci-Fi'],
},
{
name: 'Incredibles 2',
genre: ['Action' , 'Crime' , 'Drama' , 'Thriller']
},
{
name: 'Deadpool 2',
genre: ['Action' , 'Adventure' , 'Comedy']
}
];
I'm trying to traverse through the object's array and find matches using the following code, but it doesn't seem to be working as expected. How I can find matches between objects based on genre?
for (let i = 0; i < films.length; i++) {
let film = films[i];
let genres = film.genre;
for (let j; j < genres.length; j++) {
if (genres[j] == "Action") {
console.log('Match');
} else {
console.log('No Match');
}
}
}
Upvotes: 1
Views: 98
Reputation: 92440
I'm working on the assumption that the two different property names is not a typo and that you have genre
and dependencies
.
Either way, you can do it with a one-liner:
const films = [{name: 'Ant-Man and the Wasp',genre: ['Action' , 'Adventure' , 'Sci-Fi' , 'Comedy']},{name: 'Sorry to Bother You',genre: ['Comedy' , 'Fantasy']},{name: 'Jurassic World: Fallen Kingdom',dependencies: ['Action' , 'Adventure' , 'Sci-Fi'],},{name: 'Incredibles 2',dependencies: ['Action' , 'Crime' , 'Drama' , 'Thriller']},{name: 'Deadpool 2',dependencies: ['Action' , 'Adventure' , 'Comedy']}];
let actionflicks = films.filter(f => (f.genre || f.dependencies).includes( 'Action'))
console.log(actionflicks)
As for your code, it's not a bad start, but it is failing with errors. You should get in the habit of looking at the console when things aren't working. It will point you toward what's wrong.
Upvotes: 7
Reputation: 46
The key in other objects are different.In the last 3 objects the key is 'dependencies' instead of 'genre' .So when traversing last 3 objects films[i].genre
is undefined
,so there will be a error at genres.length
If you want also traverse through 'dependencies' then use the following code
for (let i = 0; i < films.length; i++) {
let film = films[i];
let genres = film.genre||film.dependencies;//if no 'genre' then check 'dependencies'
if(!genre){continue;}//if there is none then skip the loop
for (let j; j < genres.length; j++) {
if (genres[j] == "Action") {
console.log('Match');
} else {
console.log('No Match');
}
}
}
Upvotes: 1
Reputation: 935
Apply condition before looping the genres
for (let i = 0; i < films.length; i++) {
let film = films[i];
let genres = film.genre;
if (genres != undefined){
for (let j; j < genres.length; j++) {
if (genres[j] == "Action") {
console.log('Match');
} else {
console.log('No Match');
}
}
}
}
Upvotes: 1
Reputation: 993
There are lot of typos in this code. Correct code should be:
const films = [
{
name: 'Ant-Man and the Wasp',
genre: ['Action' , 'Adventure' , 'Sci-Fi' , 'Comedy']
},
{
name: 'Sorry to Bother You',
genre: ['Comedy' , 'Fantasy']
},
{
name: 'Jurassic World: Fallen Kingdom',
genre: ['Action' , 'Adventure' , 'Sci-Fi'],
},
{
name: 'Incredibles 2',
genre: ['Action' , 'Crime' , 'Drama' , 'Thriller']
},
{
name: 'Deadpool 2',
genre: ['Action' , 'Adventure' , 'Comedy']
}
];
for (let i = 0; i < films.length; i++) {
let film = films[i];
let genres = film.genre;
for (let j = 0; j < genres.length; j++) {
if (genres[j] == "Action") {
console.log('Match');
} else {
console.log('No Match');
}
}
}
As Mark pointed out, some objects in array have properties called "dependencies" which should be called "genre". Also "for (let j; j < genres.length; j++) {", j must be assigned a value.
Upvotes: 1