Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
How to get just value from an array of object with condition?

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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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]));

about 4 years ago · Juan Pablo Isaza Relatório

0

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); 
 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda