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

131
Vistas
Asigne a través de una matriz de objetos para filtrar una matriz en JavaScript

Tengo una identificación y con eso quiero mapear a través de una matriz y obtener una nueva matriz donde solo estarán allí los elementos de identificación que contienen.

Entonces, la matriz es como:

 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 } ]

Ahora, supongamos que mi identificación es 28. Solo quiero los elementos que contienen la identificación 28. Entonces, mi resultado debería ser así:

 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 }, ]

Esto es lo que he intentado:

 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

Puede hacerlo usando el filter y el método de includes en la matriz, así:

 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

Supongo que tienes una variable llamada ID

Usar filter para devolver una nueva matriz después de filtrar

La condición es genre_ids incluir 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