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
.
CodePudding user response:
const genresHandler = (movieId) => {
const movie = movieGen.find((movie) => movie.id === movieId);
return movie ? movie.name : null;
}
CodePudding user response:
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]));
CodePudding user response:
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);
CodePudding user response:
Find the object using find()
. Return a specific property :
const movieGen = [{id:28, name: "Action" }, {id:12,name:"Adventure"}, { id: 16, name: "Animation"}];
const genresHandler = (movieId) =>
movieGen.find((movie) => movie.id === movieId)?.name ?? ''
console.log(genresHandler(28));
console.log(genresHandler(29));
console.log(genresHandler(12));
You can use optional chaining
to handle cases when item does not exist. With optional chaining you can try to access nested properties (which might not exist) of an object without nested if conditions
CodePudding user response:
const genresHandler = (id) => {
const found = movieGen.find(movie => movie.id === id)
// if found, return the name, if not, return null
return found ? found.name : null
}
CodePudding user response:
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" }
]
var data = movieGen.filter(item => item.id === parseInt(28)); // pass your value instade of 28
data.map(matchItem => console.log(matchItem))