I am trying to filter name of genres for a movie based on ids. so if id matches I get that name out.
const genres = [
{ id: 28, name: "Action" },
{ id: 12, name: "Adventure" },
{ id: 16, name: "Animation" },
{ id: 35, name: "Comedy" },
{ id: 80, name: "Crime" },
{ id: 99, name: "Documentary" },
{ id: 18, name: "Drama" },
{ id: 10751, name: "Family" },
{ id: 14, name: "Fantasy" },
{ id: 36, name: "History" },
{ id: 27, name: "Horror" },
{ id: 10402, name: "Music" },
{ id: 9648, name: "Mystery" },
{ id: 10749, name: "Romance" },
{ id: 878, name: "Science Fiction" },
{ id: 10770, name: "TV Movie" },
{ id: 53, name: "Thriller" },
{ id: 10752, name: "War" },
{ id: 37, name: "Western" },
];
const genre_ids = [10752, 10770, 18, 12]
CodePudding user response:
You can filter genres
using genre_ids
and then map the specific values
function filterGenres(genres, ids) {
return genres
.filter((genre) => ids.indexOf(genre.id) != -1)
.map((genre) => genre.name)
}
or alternatively use the reduce
function
function filterGenres(genres, ids) {
return genres.reduce((results, genre) => {
if(ids.indexOf(genre.id) != -1) {
results.push(genre.name);
}
return results;
}, []);
}
CodePudding user response:
The below may be one possible solution to achieve the desired objective.
Code Snippet
const getNames = (ids, objects) => (
ids.map(el => (
objects.find(
({id}) => id === el
)?.name
))
);
// below will be better in performance
// but the names returned will be ordered based on 'genres' array
// objects.filter(({id}) => ids.some(el => el === id))
// .map(({name}) => name)
const genres = [
{ id: 28, name: "Action" },
{ id: 12, name: "Adventure" },
{ id: 16, name: "Animation" },
{ id: 35, name: "Comedy" },
{ id: 80, name: "Crime" },
{ id: 99, name: "Documentary" },
{ id: 18, name: "Drama" },
{ id: 10751, name: "Family" },
{ id: 14, name: "Fantasy" },
{ id: 36, name: "History" },
{ id: 27, name: "Horror" },
{ id: 10402, name: "Music" },
{ id: 9648, name: "Mystery" },
{ id: 10749, name: "Romance" },
{ id: 878, name: "Science Fiction" },
{ id: 10770, name: "TV Movie" },
{ id: 53, name: "Thriller" },
{ id: 10752, name: "War" },
{ id: 37, name: "Western" },
];
const genre_ids = [10752, 10770, 18, 12];
console.log(getNames(genre_ids, genres));
Explanation
- A new method
getNames
is used to obtain the result - For each id in
genre_ids
array - Find if there is an entry in
genres
(by matching theid
prop) - If found, return the
name
prop from the matchedgenres
element - And the resulting array is returned (implicitly)
Commented-out method:
- Filter the
genres
and destructure to obtain just theid
- Check if the
id
is present in thegenre_ids
array - If yes, use
.map
to destructure just thename
- And this resulting array of
name
s is returned (implicitly)
This method returns in a different order (based on the genres
array).
CodePudding user response:
are you trying something like this -
let genre_names = new Array();
for(let i=0; i<genre_ids.length; i ) {
var a = genres.find(x=>x.id == genre_ids[i]);
if (!!a)
genre_names.push(a.name);
}
console.log(genre_names);
resulting with [War,Tv Movies, drama, Adventure] ?
CodePudding user response:
You can simply achieve that by using Array.filter() along with Array.map().
const genres = [
{ id: 28, name: "Action" },
{ id: 12, name: "Adventure" },
{ id: 16, name: "Animation" },
{ id: 35, name: "Comedy" },
{ id: 80, name: "Crime" },
{ id: 99, name: "Documentary" },
{ id: 18, name: "Drama" },
{ id: 10751, name: "Family" },
{ id: 14, name: "Fantasy" },
{ id: 36, name: "History" },
{ id: 27, name: "Horror" },
{ id: 10402, name: "Music" },
{ id: 9648, name: "Mystery" },
{ id: 10749, name: "Romance" },
{ id: 878, name: "Science Fiction" },
{ id: 10770, name: "TV Movie" },
{ id: 53, name: "Thriller" },
{ id: 10752, name: "War" },
{ id: 37, name: "Western" },
];
const genre_ids = [10752, 10770, 18, 12];
const res = genres.filter((obj) => {
return genre_ids.indexOf(obj.id) !== -1
}).map((filteredObj) => filteredObj.name);
console.log(res);