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

69
Vistas
Compare a nested array with another array

I have two array one is Movie List and another is Genre

Requirement:

Need to compare both the array and filter the movies according to its genre.

1.Example for Movie List

movietList = [
  {
    title: "The Dark Knight",
    genre: "Action"
  },
  {
    title: "The Godfather",
    genre: "Crime"
  },
  {
    title: "The Shawshank Redemption",
    genre: "Drama"
  },
]

2.Example for Genre

genre = [Action, Crime, Drama]

3.Expected Result

result = [
  action: {
     title: "The Dark Knight"
      },
  crime: {
     title: "The Godfather"
     },
  drama: {
     title: "The Godfather"
     }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you will need array.reduce method to group movie by genre

you can have several movie with same genre so elements grouped should be place in an array []

var movieList = [
  {
    title: "The Dark Knight",
    genre: "Action"
  },
  {
    title: "The Godfather",
    genre: "Crime"
  },
  {
    title: "The Shawshank Redemption",
    genre: "Drama"
  },
  {
    title: "The Shawshank Redemption 2",
    genre: "Drama"
  },
];

let result = movieList.reduce((acc, current) => {
  const genre = current.genre.toLowerCase();
  (acc[genre] = acc[genre] || []).push({
    title: current.title
  });
  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const movietList = [
    {
        title: "The Dark Knight",
        genre: "Action"
    },
    {
        title: "The Godfather",
        genre: "Crime"
    },
    {
        title: "The Shawshank Redemption",
        genre: "Drama"
    },
];

const genres = ["Action", "Crime", "Drama"];


const groupBy = (movietList.reduce((acc, {title, genre}) => {
    if (!genres.includes(genre)) {
        acc[genre] = []
    }
    else {
        acc[genre.toLowerCase()] = {title}
    }
    return acc;
}, {}));


console.log(Object.entries(groupBy).flat());

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