I have a question. I am trying to write but failed. I need your help. Please help me.
Suppose I have a array of object-
const movieGen = [
{ 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" }
]
Here I need one function. That function should be worked as when id is matched then return the name. Like-
const genresHandler = (id: number) => { ....//Here should be the function }
When I provide 28 as id to this function it should return Action
console.log(genresHandler(28)) output should be: Action
console.log(genresHandler(12)) output should be: Adventure
Please help me to write this function in typscript or javascript.
const genresHandler = (movieId) => {
const movie = movieGen.find((movie) => movie.id === movieId);
return movie ? movie.name : null;
}
Best way to traverse the movies inside the movieGen array is to use the find function which takes a callback function that compares and returns the first movie entry that contains an id that is the same as the one that is passed into the genresHandler function (movieId). The result of find is then placed inside a ternary operator which returns the name of the movie if one has been found (is defined), or null if one has not been found.
Use find to locate the object with the matching id and return the name value.
The ? refers to a relatively new feature in JavaScript called optional chaining. It basically means if the object is found return the name otherwise return a default message.
const arr=[{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"}];
function getGenre(arr, id) {
return arr.find(movie => {
return movie.id === id;
})?.name || 'No genre found';
}
console.log(getGenre(arr, 28));
console.log(getGenre(arr, 12));
console.log(getGenre(arr, 1));
If you wanted to be fancy you could pass in an array of ids and get all the genres. You would filter out the objects where the ids array contains the movie id (returns an array of movie objects), and then map to return only the names from those objects.
const arr=[{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"}];
function getGenre(arr, ids) {
return arr
.filter(movie => ids.includes(movie.id))
.map(movie => movie.name);
}
console.log(getGenre(arr, [28, 12, 9648, 1]));
in JavaScript:
const movieGen = [
{ 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" }
];
function test(id) {
const res = movieGen.filter(el => el.id == id);
console.log(res[0].name);
}
test(28);