Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Map though an objects array to filter an Array in JavaScript

I have an id and with that i want to map through an array and get a new array where only the containing id items will be there.

So,the array is like :

var list = [
     {
      genre_ids: [28, 12, 878],
      genres: undefined,
      id: 634649,
      image:        "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg",
      popularity: 12971.91,
      released: "2021-12-15",
      title: "Spider-Man: No Way Home",
      vote_average: 8.5,
      vote_count: 2295
    },
    { 
      genre_ids: [878, 28, 12],
      genres: undefined,
      id: 580489,
      image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg",
      popularity: 6303.047,
      released: "2021-09-30"
      title: "Venom: Let There Be Carnage"
      vote_average: 7.2
      vote_count: 5012
     },
     {
      genre_ids: [16, 35, 10751, 14],
      genres: undefined,
      id: 568124,
      image: "http://image.tmdb.org/t/p/w342//4j0PNHkMr5ax3IA8tjtxcmPU3QT.jpg",
      popularity: 3157.171,
      released: "2021-11-24",
      title: "Encanto",
      vote_average: 7.6,
      vote_count: 541
     }
    ]

Now, suppose my id is 28. I want only the items containing id 28. So, my output should be like this:

    var list = [
         {
          genre_ids: [28, 12, 878],
          genres: undefined,
          id: 634649,
          image:        "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg",
          popularity: 12971.91,
          released: "2021-12-15",
          title: "Spider-Man: No Way Home",
          vote_average: 8.5,
          vote_count: 2295
        },
        { 
          genre_ids: [878, 28, 12],
          genres: undefined,
          id: 580489,
          image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg",
          popularity: 6303.047,
          released: "2021-09-30"
          title: "Venom: Let There Be Carnage"
          vote_average: 7.2
          vote_count: 5012
         },
         
        ]

Here's what i have tried:

 var newGenre = list.map((item)=>{
      return item.genre_ids.map((gId)=>{
        gId === 28
      })
 })

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do it by using filter and includes method on array, like this:

let list = [{ genre_ids: [28, 12, 878], genres: undefined, id: 634649, image: "http://image.tmdb.org/t/p/w342//1g0dhYtq4irTY1GPXvft6k4YLjm.jpg", popularity: 12971.91, released: "2021-12-15", title: "Spider-Man: No Way Home", vote_average: 8.5, vote_count: 2295 }, { genre_ids: [878, 28, 12], genres: undefined, id: 580489, image: "http://image.tmdb.org/t/p/w342//rjkmN1dniUHVYAtwuV3Tji7FsDO.jpg", popularity: 6303.047, released: "2021-09-30", title: "Venom: Let There Be Carnage", vote_average: 7.2, vote_count: 5012, }, { genre_ids: [16, 35, 10751, 14], genres: undefined, id: 568124, image: "http://image.tmdb.org/t/p/w342//4j0PNHkMr5ax3IA8tjtxcmPU3QT.jpg", popularity: 3157.171, released: "2021-11-24", title: "Encanto", vote_average: 7.6, vote_count: 541 } ];
const targetId = 28;
let result = list.filter(item => item.genre_ids.includes(targetId));
console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

I suppose that you have a variable called ID

Using filter to return new array after filtering

The condition is genre_ids include ID

const ID = 28
const filteredList = list.filter(item => item.genre_ids.includes(ID))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda